Data Structure and Algorithm Chapter Three: Linear Array

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Data Structure and Algorithm

Chapter Three

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 hold.


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;
Marks[4]=30;
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


element in array_name marks, we give the following
statement respectively: cin>>marks[3];
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.
1. Start
2. IF N = MAX, return
3. ELSE
N=N+1
SEEK Location k
For All Elements from A[k] to A[N]
Move to next adjacent location
4. A[k] = x
5. Exit
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> for(i=0;i<n;i++)

#define MAX 15 {

cout<<"Enter the "<<i+1<<" element of the array: ";


using namespace std;
cin>>a[i];
int main()

{ }
cout<<"\n\nStored Data in array: ";
int i,a[MAX],no,pos,n, y;
for(i=0;i<n;i++)
y= MAX-3; {

cout<<"Enter the number of elements(max # of cout<<a[i]<<" ";

Elements is"<<y<<endl; }
cout<<"\n\nEnter position number of your insertion: ";
cin>>n;
cin>>pos;
if(n>y) if(pos>n)
{
cout<<“the max size of the array is <<y<<endl;
cout<<"\n\nThis is out of range";
else { }
Cont.
else
{
cout<<"\n\nEnter new number: ";
cin>>no;
--pos;
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=no;
La insertion2.cpp cout<<"\n\nNew data in array: ";
for(i=0;i<n+1;i++)
{
cout<<a[i]<<" ";
}
}
}
return 0;
}
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’

1.Start

2. Set N=k+1

3. While N<= upper bound

Set A[N-1]=A[N]

N=N+1

4. Exit
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> else
#define MAX 15 {

using namespace std; for(i=0;i<n;i++)


int main() {
{ cout<<"Enter the "<<i+1<<" element of the array: ";
int i,a[MAX],no,pos,n,y; cin>>a[i];
y= MAX-3; }
cout<<"Enter the number of elements ( cout<<"\n\nStored Data in array: ";
Max number of element is )"<<y<<endl;
for(i=0;i<n;i++)
cin>>n;
{
if (n> y)
cout<<a[i]<<" ";
cout<< "the maximum size of thearray is
"<<y; }
Cont.

cout<<"\n\nEnter position the element to delete: "; cout<<"\n\nNew data in array: ";
cin>>pos; for(i=0;i<n-1;i++)
if(pos>n) {
cout<<"\n\nThis is out of range"; cout<<a[i]<<" ";
else }
{ }
--pos; }
for(i=pos;i<n;i++) return 0;
{ }
a[i]=a[i+1];
}

La deletion 2.cpp
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’

1. Start

2. Set k=lower bound

3. Repeat while k<=upper bound


If A[k]=x
return k and exit
Else
k=k+1
4. Return k=-1

5. Exit
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> while(j<n){
if(a[j]==item){
using namespace std;
break;
int main() }
{ j=j+1;
}
int i, n=5, item=4, j=0; if(j>=n){
int a[]={2,4,6,4,10}; cout<<"\n\n element "<<item<<" not
found "<<endl;
cout<<"\n\nOriginal elements }
of the array are "; else{
for(i=0;i<n;i++) cout<<"\n\nfound element "<<item<<" at
position "<<j+1<<endl;
{
}
cout<<a[i]<<" "; cout<<"\n\nfound element "<<item<<" at position
} "<<j+1<<endl;
return 0;
} LA search.cpp
Implementation
• search for array element based on its value
#include<iostream> for(i=0; (i<m) && (A[i] !=find); i++)
using namespace std; continue;
int main() if(i==m)
{ cout<<"\n\n"<<find<<" is not in the
intA[] = {8,25,36,44,52,60,75,89}; list"<< endl;

int find; else

int i, m=8; cout<<"\n\n"<<find<<" is the “ << i+1


<< "th element in the list\n\n";
cout<<"\nenter a number to
search\n\n"; }

cin>>find;

LA search2.cpp
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.

1. Start

2. Set A[K-1] = ITEM

3. Stop
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> a[k-1]=item;
cout<<"\n\nUpdated elements
using namespace std;
of the array are ";
int main()
{ for(i=0;i<n;i++)
{
int i, n=5, item=7, j=0, k=3; cout<<a[i]<<" ";
int a[]={2,4,6,8,10}; }
return 0;
cout<<"\n\nOriginal elements of the array are "; }
for(i=0;i<n;i++)
{
LA update.cpp
cout<<a[i]<<" ";
} LA search and update.cpp
Ex.
• Write an algorithm and C++ implementation that
search and update a linear array

You might also like