Data Structure and
Algorithm
Chapter Five
Linear Array
Introduction
• Array is a container which can hold fixed number of items
and these items should be of same type.
• Most of the data structure make use of array to implement
their algorithms. Following are important terms to
understand the concepts of Array.
– Element − Each item stored in an array is called an
element.
– Index − Each location of an element in an array has a
numerical index which is used to identify the element.
Cont.
• The index of first element is known as lower bound and the
index of the last element is known as upper bound.
Declaration of Linear array :
data_type array-name[size];
where data_type base type of array
array-name the name the
array
size how many elements the array will
Cont.
Example int marks[10];
• The above statement declared an integer array marks with
10 elements, marks[0] to marks[9].
Initialization of array :
data_type array-name[size] = { element-1, element-2 , … , element-
n}; or
data_type array-name[] = {element-1,element-2 , … … . . , element-
n};
Cont.
Example
int marks[5]={50,25,72,45,30};
Marks[0]=50;
Marks[1]=25;
Marks[2]=72;
Marks[3]=45;
Accessing Linear Array
Elements
• Individual element of an array can be accessed using
the
following syntax :
array_name[index or subscript];
• For example, to assign a value to second location of array,
we give the following statement : marks[1]=90;
• Similarly, for inputting and outputting the value of
fourth
statement respectively: cin>>marks[3];
element in array_name marks, we give the
following cout<<marks[3];
Cont.
• Arrays can also be accessed through loop. Two
different
loops are required for inputting into and outputting
from array.
Input to the array Output from the array
Basic Operations
The following are the basic operations supported by
an array.
• Traverse − print all the array elements one by one.
• Insertion − add an element at given index.
• Deletion − delete an element at given index.
• Search − search an element using given index or by value.
• Update − update an element at given index.
Insertion Operations
• Insert operation is to insert one or more data elements into an
array. Based on the requirement, new element can be added
at the beginning, end or any given index of array.
• An operation of adding an element in a linear array
Cont.
• There should be a memory location to add a new element so
the index of a newly inserted element must be less than or
equal to the upper bound
• To insert an element ‘x’ in linear array ‘A’ at index
k<=upper bound
• First we should create an empty space of index
k to accommodate element x.
• So to create an empty space the last element should move
to the next higher index until element e moves.
Cont.
Algorithm
• Algorithm to insert an element ‘x’ in linear array ‘A’ with n
elements at index k<=upper bound. The maximum number of
elements it can store is defined by MAX.
Start
IF n = MAX, return
ELSE
SEEK Location k
For All Elements from A[k] to
A[n] Move to next adjacent
location
A[k] = x
n=n+1
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream>
#define MAX 10
using namespace std;
void insertA(int a[], int& n);
int main() cout<<"\n\nStored Data in array: ";
{ for(i=0;i<n;i++)
int i, A[MAX],n, y; {
y= MAX-3; cout<<A[i]<<" ";
cout<<"Enter the number of elements(max }
# of Elements is "<<y<<endl;
cin>>n; insertA(A,n);
if(n>y)
cout<<"the max size of the array is" cout<<"\n\nNew data in array: ";
<<y<<endl; for(i=0;i<n;i++)
else { {
for(i=0;i<n;i++) cout<<A[i]<<" ";
{ }
cout<<"Enter the "<<i+1<<" element of }
the array: ";
cin>>A[i]; return 0;
} }
Cont.
void insertA(int a[], int& n)
{
int pos, num,i;
cout<<"\n\nEnter position number of your
insertion: ";
cin>>pos;
if(pos>n+1 || pos<1)
{
cout<<"\n\nThis is out of range";
}
else if(pos==n+1)
{ else
--pos; {
cout<<"\n\nEnter new number: "; cout<<"\n\nEnter new number: ";
cin>>num; cin>>num;
a[pos]=num; --pos;
n++; for(i=n-1;i>=pos;i--)
} {
a[i+1]=a[i];
}
a[pos]=num;
n++;
}
}
Deletion Operation
• Deletion refers to removing an existing element from
the array and re-organizing all elements of an array.
• Removing an element in a linear array.
• If we want to delete the last element we can directly delete it;
no need of adjusting its index.
Algorithm
• Algorithm to delete element A[k] of linear array of ‘A’
where ‘k’ is less than upper bound of ‘A’
Start
Set counter=k
While counter< upper bound
Set A[counter]=A[counter +1]
N=N+1
Stop
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream>
#define MAX 10
using namespace std;
void deleteA(int a[], int& n);
int main() cout<<"\n\nStored Data in array: ";
{ for(i=0;i<n;i++)
int i, A[MAX],n, y; {
y= MAX-3; cout<<A[i]<<" ";
cout<<"Enter the number of elements(max }
# of Elements is "<<y<<endl;
cin>>n; deleteA(A,n);
if(n>y)
cout<<"the max size of the array is" cout<<"\n\nNew data in array: ";
<<y<<endl; for(i=0;i<n;i++)
else { {
for(i=0;i<n;i++) cout<<A[i]<<" ";
{ }
cout<<"Enter the "<<i+1<<" element of }
the array: ";
cin>>A[i]; return 0;
} }
Cont.
void deleteA(int a[], int& n)
{
int pos,i;
cout<<"\n\nEnter position number of
your deletion: ";
cin>>pos;
if(pos>n || pos<1)
{
cout<<"\n\nThis is out of range";
}
else if(pos==n)
{ else
--pos; {
a[pos]=0;
n--; --pos;
} for(i=pos;i<n;i++)
{
a[i]=a[i+1];
}
a[n-1]=0;
n--;
}
}
Implementation
• The above implementation accepts the position of deletion
from the user.
• Write an implementation that accepts the element to be
deleted, searches for it and delete it.
Search Operation
• You can perform a search for array element based on
its value or its index.
• Operation of finding the location of an element in a linear
array
• Linear search is also called sequential search.
• Start from the beginning and check one by one
Algorithm
• Algorithm to find element ‘x’ in linear array
‘A’
Start
Set k=lower bound
Repeat while k<=upper bound
If A[k]=x
return k and exit
Else
k=k+1
Return k=-1
Stop
Implementation
• Below is an example of implementation of the above algorithm.
while(j<n){
#include<iostream> if(a[j]==item){
using namespace std; break;
}
int main()
j=j+1;
{ }
if(j>=n){
int i, n=5, item=4, cout<<"\n\n element "<<item<<" not
found "<<endl;
j=0; int }
else{
a[]={2,4,6,4,10};
cout<<"\n\nOriginal cout<<"\n\nfound element "<<item<<" at
elements "<<j+1<<endl;
of the array are ";
for(i=0;i<n;i++) }
{ cout<<"\n\nfound element "<<item<<" at positio
"<<j+1<<endl;
cout<<a[i]<<" "; return 0;
} }
Implementation
• search for array element based on its value
#include<iostream>
using namespace for(i=0; (i<m) && (A[i] !=find); i++)
std; int main() continue
{
;
intA[] =
{8,25,36,44,52,60, if(i==m)
75,89}; cout<<"\n\n"<<find<<" is not
in the list"<< endl;
int find;
else
int i, m=8;
cout<<"\n\n"<<find<<" is the
cout<<"\nenter a number
“ << i+1
to search\n\n";
<< "th element in the list\n\n";
cin>>find;
}
Update Operation
• Update operation refers to updating an existing element from
the array at a given index.
Algorithm
• Consider A is a linear array with N elements and K is a
positive integer such that K<=N. Below is the algorithm to
update an element available at the Kth position of A.
Start
Set A[K-1] = ITEM
Stop
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream>
using namespace
std; int main()
{
int i, n=5, item=7, j=0, a[k-1]=item; cout<<"\n\
k=3; int nUpdated elements
of the array are ";
a[]={2,4,6,8,10};
cout<<"\n\nOriginal
elements of the array for(i=0;i<n;i+
are "; +)
{
for(i=0;i<n;i++) cout<<a[i]<<"
{ ";
}
cout<<a[i]<<" "; return 0;
}
Ex.
• Write an algorithm and C++ implementation
that search and update a linear array