You are on page 1of 32

ARRAYS

ADDRESS CALCULATION Address in Row major order Address of element in row major order [I,J]=B+W[n(I-Lr)+(J-Lc)] Where B=Base Address W=Width of an element n = number of columns I&J are the subscripts of the element.

Lr is the Lower bound of the row Lc is the lower bound of the column Generally in C++ we take the Lr &Lc as 0 but it can also be taken as 1.

Example : A is a 2-D array[10*5] . Each element of the array is stored in 2 memory locations. The base address is 150, find the location of A[3,4] if the arrangement is in row major order. Solution : B = 150 W=2 n=5 Taking Lr &Lc as 0

Address of A[3,4] = B+W[n(I-0) +(J-0)] = 150+2[5(3-0)+(4-0)] = 150+2[5(3)+(4)] = 150 +2[15+4] =150 +2[19] = 150+38 =188 Ans.

Exercise : Q1. An array Arr[15][20] is stored in the memory along the row with each element occupying 4 bytes with the base address as 1000. Find out the address of the element Arr[3][2]. Q2.An array Ar[50][100] is stored in the memory along the row with each element occupying 2 bytes . Find out the address of the location Arr[20][50] if the location Arr[10][25] is stored at the address 10000.

Example : Q. A 2-D array defined as A[4..7,-1..3] requires 2 words of storage space For each element. If the array in Row-major form,calculate the address of A[6,2] given the base address as 100. Solution: B=100 W=2 bytes Lr=4,Lc=-1 No. of rows are calculated (m) as : Ur-Lr+1 = 7-4+1=4 No. of columns are (n) : Uc-Lc+1 = 3-(-1)+1 = 3+1+1=5 Address[I,J] = B +W(n(I-Lr)+(J-Lc)) =100+2(5(6-4)+2-(-1)) =100+2(5(2)+3) = 100+2(10+3) =100+2(13) =100+26 =126

COLUMN MAJOR : Address(I,J) = B+w[(I-lr)+m(J-Lc)] r = number of rows Example : Q. An array X[50][20] is stored in the memory along with each of the element occupying 2 bytes. Find out the memory location for the element P[10][15],if the Base Address of the array is 6800. Solution W=2 B=6800 M(no. of rows) = 50 Lr=0,Lc=0 Address of P[10][15] = B+W[(I-Lr) +m(J-Lc)] =6800+2[10-0)+50(15-0)] = 6800+2[10+50(15)] =6800+2[10+750] = 6800+2[760] =6800+1520 =8320

Searching in Arrays

1.Linear Search
2.Binary Search

Exercise : Q 1. If an Array[10][20] is stored as column major and base address is 900. Find the address of Array[5][12] if each element takes 4 bytes. Q 2.An array VAL[1..15][1..10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of array VAL is 1500, Determine the location of VAL[12][9] when the array VAL is stored i)Row wise ii)Column wise Q 3.An array Arr[40][30] is stored in the memory along the column with each of the element occupying 2 bytes. Find out the base address and address Of the element S[20][15], if an element Arr[12][10] is stored at the memory location 7200.

In Linear Search method each element of the array is compared with the given data to be searched one by one. This method which traverses the array sequentially to locate the given data. Algorithm : 1. Ctr = L (Lower bound is 0) 2. Repeat steps 3 through 4 until ctr>U (U= Upper bound =size -1) 3. If AR[ctr] == ITEM then { print Search Successful print ctr,is the location of,ITEM break } 4. ctr = ctr+1 5. If ctr > U then print Search Unsuccessful 6. END

Linear Search

Program for Linear Search : #include <iostream.h> int lsearch(int [ ] , int,int); void main( ) { int AR[50],ITEM,N,index; cout<<Enter desired aaray size; cin>>N; for(int i=0;i<N;i++) { cin>>AR[i]; cout<<Enter the element to search; cin>>ITEM; Index=Lsearch(AR,N,ITEM); if(index ==-1) cout<<The item to search does not exist; else cout<<Element found;

int Lsearch(int AR[ ],int size,int item) { for(int i=0; i<size;i++) { if(AR[i] == ITEM) return; } return -1;

Binary Search

This method searches the element in minimum possible combinations. In Binary search method the array should be sorted in any order. The element to search is compared with the middle element first,If the element is greater than the middle element it is searched in the latter part of the array,else it os search in the first part of the array. Algorithm 1. Set beg = L,Last = U 2. Repeat steps 3 through 6 UNTIL beg>last 3. Mid =( beg+last)/2 4. If AR[mid] ==ITEM then print Search Successful break 5 If AR[mid]<ITEM then big = mid +1 6. If AR[mid] >ITEM then last = mid-1 7. If beg!=last print Search Unsuccessful 8. END

Program for Binary Search #include<iostream.h> int bsearch(int [ ],int,int); void main( ) { int AR[50],ITEM,N,index; cout<< Enter desired array size ; cin>> N; cout<<Enter Array elements; for(int i=0;i<N;i++) cin>>AR[i]; cout<<Enter element to search for ; cin>>ITEM; index = Bsearch(AR,N,ITEM); If(index == -1) cout<<Element not found; else cout<<Element found; }

int Bsearch(int AR[ ],int size,int item) { int beg,last,mid; Beg=0; while(beg<=last) { mid = (beg+last)/2; if(item ==AR[mid]) return mid; else if(item>AR[mid]) beg = mid+1; else last = mid-1; } return -1 // if item not found

INSERTION IN AN ARRAY

Algorithm: Assuming U as Upper bound ,N is No. of elements in the array 1. Ctr =0 2. Print Enter the elements of the array 3. Repeat steps 4 to 5 until ctr < U 4. Input Ar[ctr] 5 ctr=ctr+1 end of repeat 6. Print Enter the number to insert in the array 7.Input Num 8. ctr=0 9.Repeat steps 10 until ctr < U and AR[ctr]<Num 10 ctr++ End of repeat 11. If Ar[ctr]> Num j=U Ar[j] = Ar[ctr] j- `` ````````````````````

//Printing the array 12 ctr=0 13Repeat steps 14 to 15 until ctr <N+1 // N is the number of elements input by the user 14. Print AR[ctr] 15 ctr++ End of repeat 16. END Q. Write a program to insert an element in a single dimension array.

DELETION Assuming the array is already created 1. ctr=0 2. Print Enter the element to delete 3. Input Data 4. Repeat step 5 until AR[ctr]<=Data 5. ctr++ End of repeat 6. If AR[ctr] ==Data Repeat steps 7 to until ctr <= N 7 AR[ctr] = AR[ctr+1] 8 ctr=ctr+1 end of repeat Displaying the array after deletion 9 ctr = 0 10. Repeat steps 11 to 12 until ctr<N-1 11 Print AR[ctr] 12 ctr=ctr+1 end of repeat 13 END

Q. Write a program to delete an element from an array Q. Write a program to find the sum of all columns in a two dimensional array Q. Write a program to create a two dimensional array and find the sum of those elements which are divisible by 5.

Sorting of the Array 1. Selection Sort 2. Bubble Sort 3. Insertion Sort

Selection Sort Selection Sort repeatedly selects the smallest key in the remaining unsorted array.

ALGORITHM : 1. small =ar[L]; 2. Repeat steps 3 to 10 until I <U { 3. small =ar[I] 3. j =0 4. Repeat steps 4 to 8 until J<U 5. If ar[J] <small then 6. { small = ar[J] 7. pos =J } J=J+1 } End of repeat (j<u) 8. temp = AR[I] 9.ar[I]=small 10.AR[pos] = temp } End of repeat (i<U) 11. END.

Selection Sort function void selsort(int AR[ ],int size) { int small,pos,tmp; for(int i=0;i<size;i++) { small = Ar[i]; for(int j=j+1;j<size;j++) { if(Ar[j]<small) { small = Ar[j]; pos =j; }

tmp = Ar[i]; AR[i] =AR[pos] AR[pos] = tmp; cout<<\nArray after pass <<i+1<<-is:; for(j=0;j<size;j++) cout<<AR[j]<<; } }

Bubble Sort The basic idea of bubble sort is to compare two adjoining values and exchange them if they are not in proper order.

Bubble Sort Algorithm 1.Repeat steps 2 to4 until I < U 2.J=L 3.Repeat step 4 until J<U 4.If( Ar[j] > Ar[j+1] then { temp = AR[j] Ar[j] = Ar[j+1] Ar[j+1] =temp } End of if End of repeat (j loop) 5. END.

Bubble sort function void bubblesort(int AR[ ] ,int size) { int tmp,ctr=0; for(int i=0;i<size;i++) { for(int j=0;j<size -1;j++) { if(Ar[j] > Ar[j+1]) { tmp = Ar[j]; Ar[j] = Ar[j+1]; Ar[j+1] = tmp; } } cout<<Array after iteration <<++ctr<<-is; for(int k=0;k<size;k++) cout<<Ar[k]<< ; cout<<endl; } }

Insertion Sort The insertion sort algorithm scans A from A[1] to A[N],inserting each element into its proper position in the previously sorted subarray.

Program on Insertion sort void insort(int Ar[ ],int) { int tmp,j; Ar[0] = INT_MIN;// to hold the first element the senitel pass holding minimum possible integer which is -32768 for(int i=1;i<=size;i++) { tmp = Ar[i]; j=i-1; while(tmp<Ar[j]) { Ar[j+1]=Ar[j]; j--; } Ar[j+1]=tmp; cout<<Array after pass<<i<<is; for(int k=1;k<=size;k++) cout<<Ar[k]<< ; cout<<endl; } }

You might also like