Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Arrays

Algorithm for traversing an


array
Traverse(LA,LB,UB):
(Traversing a Linear Array) Here LA is a linear
array with lower bound LB and upper bound
UB. This algorithm traverses LA applying an
operation PROCESS to each element of LA.
1. [Initialize counter] Set K := LB.
2. Repeat steps 3 and 4 while K <= UB.
3. [Visit element] Apply PROCESS to LA[K].
4. [Increment counter] Set K := K + 1.
[End of Step 2 loop.]
5. Exit.
Array Insertion algorithm
INSERT (LA, N, K, ITEM)
Let LA be a Linear Array (unordered) with N elements
and K is a positive integer such that K<=N.
Following is the algorithm where ITEM is inserted into the
Kth position of LA −
1. Start
2. Set J = N-1
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[10]={2,3,4,5,6};
int n;
int j=5,i,k;
int item;
cin>>item>>k;
n=5;
if(k<=n)
{
j=n;
while(j>=k)
{
a[j+1]=a[j];
j=j-1;
}
a[k] =item;
cout<<k;
}
n=6;
cout<<endl<<"New Array"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
Array Deletion algorithm
DELETE (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is
a positive integer such that K <= N. This
algorithm deletes the Kth element from LA.

1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[10]={2,3,4,5,6};
int n,item;
int j=6,i,k;

cin>>k;
item=a[k];
if(k<=n)
{
j=n;
for(j=k;j<n-1;j++)
{
a[j]=a[j+1];
}

n=6;
cout<<endl<<"New Array"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
Linear Search
Linear Search (Array A, Lower Bound, Value x) :
1. Set i =Lower Bound
2. if i > n then go to step 7
3. if A[i] = x then go to step 6
4. Set i = i + 1
5. Go to Step 2
6. Print Element x Found at index i and go to
step 8
7. Print element not found
8. Exit
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{

int arr[10], i, num, n, c=0, pos;


cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
getch();
}
Binary Search
BINARY(DATA,LB,UB,ITEM,LOC)
DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is
a given item of information. The variable BEG is the beginning END is end
and MID is middle locations of a segment of element of DATA. This
algorithm finds the location LOC of ITEM in DATA or sets LOC=NULL.
1. Set BEG:=LB, END:=UB and MID=INT((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!= ITEM.
3. IF ITEM<DATA[MID], then:
Set END:= MID-1
ELSE:
Set BEG:=MID+1
[END of IF structutre]
4. Set MID:=INT((BEG+END)/2
[END of step 2 Loop]
5. If DATA[MID]=ITEM, then
Set Loc:=MID.
Else:
Set Loc:=NULL
[End of IF structure]
6. Exit.
Bubble Sort
(Bubble Sort)BUBBLE(DATA,N):
DATA is an array with N elements. This algorithm sorts the
elements in DATA.

1.Repeat Steps 2 and 3 for K=1 to N-1


2. Set PTR:=1
3. Repeat while PTR<=N-K:
(a) If DATA[PTR]>DATA[PTR+1], then:
Interchange DATA[PTR] and DATA[PTR+1].
[End of IF structure]
(b) Set PTR:=PTR+1
[End of inner loop]
[End of Step 1 outer loop.]
4. Exit
Memory allocation
LOC(LA[K])=address of the element LA[K]
of the array LA.
LOC(LA[K])=Base(LA)+w(k-lowerbound)
Where :
w words per memory cell for the array.
LA Linear array
Example....
Consider the array Auto which records the
number of automobiles sold each year from
1932 through1984. Suppose AUTO appears
in memory ,Base(AUTO)=200, w=4
LOC(AUTO[1932])=200,
LOC(AUTO[1933])=204....

Calculate the address of the array element


for the year k=1965 ?
Answer
LOC(AUTO[1965])=?
BASE(AUTO)=200(GIVEN)
W=4(GIVEN)
K=1965(GIVEN)
Lower bound=1932

LOC(AUTO[1965])=Base(AUTO)+w(k-
lowerbound)
o =200+4(1965-1932)
o 332

You might also like