Your SlideShare is downloading. ×
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
1-D array
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

1-D array

500

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
500
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Page 1 of 14 ArrayAreas Covered on: 1- D ARRAY . Definition of 1-D Implementation of 1-D Basic operations of 1-D array. array array Insertion & Deletion . Searching Sorting . Merging1-D Array Definations: One dimensional array comprise of finite homogeneous elements represented as:Array name [Lower bound L, Upper bound U] ranges from L through U.To calculate array size (length) =U-L+1 Ex: Ar [-9, 15] Size=15-(-9) +1=23Implementation of 1-D array:It is done to calculate address of any element in an array.Address of element with subscript I=Base address +ES (I-L)Where ES is size of an array element L is the lower bound of the array. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 14Basic operations of 1-D array:--- 1. Insertion & Deletion 2. Searching 3. Sorting 4. MergingInsertion: placing of new element within the existing array. Insertion is done in two ways:i) If the array is unordered, placing the new element at the end of the array.ii)if the array is sorted then new element is added at appropriate position.Algorithm:1.ctr=L2.If LST=U then { Print “Overflow:” Exit from program }3. if AR[ctr]>ITEM then Pos=1 Else { 4. Repeat steps 5 and 6 until ctr>=U 5. if AR[ctr]<=ITEM and ITEM<=AR[ctr+1] then { Pos=ctr+1 Break } 6. ctr=ctr+1/*end of repeat here*/ 7. if ctr=U then Pos=U+1 } /*end of if step 3 *//* shift the element to create space*/8. ctr=U9.while ctr>=pos perform steps 10 through 1110.AR[ctr+1]=AR[ctr] Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 1411.ctr=ctr=ctr-1 }12. AR[pos]=ITEM13. ENDUsing Function: int FindPos(int AR[],int size,int item) /*to determine the position for element { int pos; if(item<AR[0]) pos=0; else { for(int i=0;i<size-1;i++) { if(AR[i]<=item && item <AR[i+1] { Pos=i+1; Break; } if(i==size-1) pos=size; } return pos; } Deletion: To delete an element from existing array. Algorithm: /*considering that Item’s search in array AR is successful at location pos*/ Two cases arises: Case1: Shifting Upward or left side 1. ctr=pos 2. Repeat steps 3 and 4 until ctr>=U 3. AR [ctr] =AR [ctr-1] 4. ctr=ctr+1 Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 14/*end of repeat*/ Case II: shifting downwards (or in right side)1. Ctr=pos2. Repeat steps 3 and 4 until Ctr<=L3.AR[ctr]=AR[ctr-1]4.ctr=ctr-1/*End of Repeat*/Searching:Linear search: Linear search method is used to scan the array in a sequentialmanner.Under this method the whole array is searched one by one until thematching element is obtained .Note: Linear search is done in Unordered array.Algorithm: /*Initialise counter by assigning lower bound value of the array*/Step1:Set ctr=L (Lower bound=0 in c++) /*Now search for the ITEM*/Step2: Repeat steps 3 through 4 until ctr>U //Upper Bound(U) is size-1Step3: if AR [ctr]==ITEM then { Print “search Successful” Print ctr,”is the location of”,ITEM Break }Step 4: ctr=ctr+1 /*End of repeat*/Step 5 if ctr>U then Print “search Unsuccessful!”Step 6 ENDUsing Function:Int LSearch(int AR[],int size,int item) { For(int i=0;i<size;i++) { Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 5. Page 5 of 14 If(AR[i]==item) return i; } return -1; } Binary search: Steps:To search for ITEM in sorted array (in ascending order) 1. ITEM is compared with middle element of the segment(i.e,in the entire array for the first time). 2.If the ITEM is more than the middle element ,latter part of the segment becomes new segment to be scanned. 3.The same process is repeated for the new segment(s) until the item is found(search successful) or the segment is reduced to the single element and the ITEM is not found(search Unsuccessful). Condition: Array should be in sorted manner Using Function: int Bsearch(int AR[10], int I, int N) { int beg=0, last = N-1,Mid; While(beg<=last) { Mid =(beg+last)/2; If (AR[Mid]==I) Return 1; Else if (I > AR[Mid]) Beg=Mid+1; Else Last=Mid-1; } return 0;}Sorting:Selection Sorting:Steps: Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 6. Page 6 of 14 Pass-1 Find the location LOC of the smallest in the list of n elements. a[1],a[2],……..a[n],then interchange a[loc]with a[1].Pass-2 Find the location LOC and then interchanged a [LOC] and a [2].since a[1]<=a[2] Pass-3 Find the location LOC of the smallest in the sublist of n-2 elements. a [3],a[4],……a[n], and then interchanged a[LOC] and a[3].Then a[1],a[2]….a[3] is stored, since a[2]<=a[3] -------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------- Pass N-1: Find the location LOC of the smallest of the elements a[n-],a[n] and interchanged a[LOC] and a[n-1].Then: a [1],a[2]….a[n] is strored, since a[n-1]<=a[n] Thus a is sorted after n-1 passesAlgorithm:1. small=AR[L]2. For i=L to U do //In c++ ,0 to size-1 {3. small=AR[i]4. for J=I to U do {5. If AR[j]<small then6. { Small==AR[j]7. Pos=j } j=j+1; } /*end of inner loop*/ /*swap the smallest element with ith element */8. temp=AR[i]9. AR[i]=small10. AR[pos]=temp }11. END Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 7. Page 7 of 14Using Function : Void SelSort(int AR[],int size) { int small, pos,tmp; for(int i=0;i<size;i++) { small=AR[i]; for(int j=i+1;j<size;j++) { if(AR[j]<small) { small=AR[j]; pos=j; } } tmp=AR[i]; AR[i]=AR[pos]; AR[pos]=tmp; cout<<”n Array after pass –“<<i+1<<”—is :”; for(j=0;j<size;j++) cout<<AR[j]<<””; }}Bubble SortThe basic idea of bubble sort is to compare two adjoining values and exchange themif they are not in proper order.Algorithm:1. For I=L to U2. { For j=to [(U-1)-I] { Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 8. Page 8 of 143. If AR[j]>AR[j+1] then {4. temp=AR[j]5. AR[j]=AR[j+1]6. AR[j+1]=temp } } }7. ENDUsing FunctionVoid BubbleSort(int AR[],int size) { int tmp,ctr=0; for(int i=0;i<size;i++) { for (int j=0;j<(size-1)-i;j++) { if(AR[j]<AR[j+1]) { tmp=AR[j]; AR[j]=Ar[j+1]; AR[j+1]=tmp; } } cout<<”Array after iteration—“<<++ctr<<”--: “; for(int k=0;k<size;k++) cout<<AR[k]<<””; cout<<endl }} Insertion sorting: Condition: Introduce a sentinel element A [0] = - ∞ (or a very small number) An insertion sort is one that sorts a set of values by inserting values into an existing sorted file. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 9. Page 9 of 14 The insertion sort algorithm scans array a from a[1] to a[n], inserting each element A[k] into its proper position on the previously sorted subarray a [1],a [2]...a [k- 1]. Using Function: Void InsSort (int AR [], int size) { int tmp,j; AR [0] =INT_MIN; 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 iteration—“<<++ctr<<”--: “; for (int k=0;k<size;k++) cout<<AR[k]<<””; cout<<endl; } } Merging:Merging means combining elements of two arrays to form a new array.Merging in array/* Merging of two array A and B where A is in ascending order ,B is in ascendingorder and resultant array C will be in Ascending ctr A=L1 ctr B=L2 ctr C=L3 while (ctr A<=U1) &&( ctr B<=U2) { If(A[ctr A]<=B[ctr B]) then Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 10. Page 10 of 14 { C[ctr C]=A[ctr A] ctr C=ctr C + 1 ctr A=ctr A+1 } else { C[ctr C]=B[ctr B] ctr C=ctr C+1 ctr B=ctr B+1 }/*end of while loop If(ctr A >U1) then { While(ctr B <=U2) { C[ctrC]=B[ctr B] ctrC=ctrC+1 ctr B=ctr B+1 }}If(ctr B>U2) then { While (ctr A<=U1) { C[ctr C]=A[ctr A] ctr C=ctr C+1 ctr A=ctr A+1 } }There are some other cases are also possible .These cases are taken place infollowing situations: 1. When array A[n] is in Acsending order then its control variable ,say ctr A ,should be initialized with array’s lower bound ,which is 0(zero).And the test condition will be ctr<n. 2. When an array ,say A[n] ,is in descending order,then its control variable ,say ctr A ,should be initialized with array’s upper bound,which is n-1. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 11. Page 11 of 14Assuming length of array A is M and Length of array B is N-1 . Thus array A haselements A[0] to A[N-1] and B has elements B[0] to B[N-1] .let the resultant array beC with length 10.CtrB=n-1 , ctrA=0 , ctrC=0while (ctrC<5) do{ C[ctrC]=A[ctrA] ctrC=ctrC+1ctrA=ctrA+1}while (ctrC<10) do{ C[ctrC]=B[ctrB] ctrC=ctrC+1ctrB=ctrB-1}ctrC=0while(ctrC<10) do{ print C[ctrC]ctrC= ctrC+1}END Solved Examples:Q.1 What do you understand by Data Structure. Describe about the types of datastructureAns A data Structure is a named group of data of different data types which can beprocessed as single unit.There are two types of data structurea. Simple :- i. Array ii. Structureb. Complex i. Linear :- i.Stack ii. Queue iii. Linked-list ii. Non-Linear : TreeQ.2What will be the address of 5th element in a floating point array implemented inC++?The array is specified as amount[16].the base address of the array is 1058. Ans) Standard size of floating-point array=4 bytes 5th element in the array Amount is specified as Amount[4] as the index numbersin c++ starts from 0. Address of Amount[4]=base address +es(I-L) L=0 Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 12. Page 12 of 14 Es=4 Base Address=1058 Putting all these values, Address of Amount[4]=1058+4(4-0)=1074Q.3 Suppose a one dimentional Array AR containing integers is arranged inascending order .Write a user defined function in C++ to search for one integerfrom AR with the help of Binary Search Method , returning an integer 0 to showabsence of the number and 1 to show the presence of the number in the array. Thefunction should have three parameters (1. an array AR 2. the number to besearched 3. the number of elements N)Ans. Int Bsearch(int AR[10], int I, int N) { int beg=0, last = N-1,Mid; While(beg<=last) { Mid =(beg+last)/2; If (AR[Mid]==I) Return 1; Else if (I > AR[Mid]) Beg=Mid+1; Else Last=Mid-1; } return 0; }Practice also For Descending orderQ.4 Write a function in C++ which accepts an integer array and its size asarguments and exchange the values of first half side element with the second halfelements of the array. Example if an array of 10 elements has initial content12,15,45,23,34,43,77,46,35,47The resultant array should be like this 43,77,46,35,47,12,15,45,23,34Ans void swap( int A[ ] ,int size) { int I,j,temp,mid=size/2; if (size %2 = = 0) j=mid ; else j=mid+1; for(i=0;i<mid;i++,j++) { temp= A[i]; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 13. Page 13 of 14 A[i]=A[j]; A[j]=temp; } }5. Write an Algorithm for Selection Sort .Ans : 1. small=AR[L] 2. for i= L to U loop 3. { small = AR[i] 4. for j = I to U do 5. { if AR[j] < small then 6. { small = AR[j] 7. pos = j } 8. j = j + 1 } 9. temp = AR[i] 10. AR[i] = small 11. AR[pos] = temp } 12. end Q.6 An algorithm requires two stacks of size M and N that are to bemaintained in the memory .Illustrate with an example how will you adjust twostacks in one dimensional array with M+N memory locations so that the overflowcondition is minimized . Ans . let us say that the stack A is with size M and Stack B with size N If the stack A is stored in locations 0 to M-1 and the stack B is stored in locationsM to M+N-1 ,the separate areas for the two are ensured .in the beginning the tops of thestacks are at opposite ends . when Top A reaches at M-1 , any further insertion will leadto overflow in stack A and when Top B reaches at M. any further insertion in stack B willlead to overflow.Q.7 Given two arrays A and B ,copy last five element of B after first five element ofA to create another array C. Assume length of A and B is greater than 5.(MergeSort)Ans Assuming length of array A is M and Length of array B is N-1 . thus array A haselements A[0] to A[N-1] and B has elements B[0] to B[N-1] .let the resultant array be Cwith length 10.ctrB=n-1 , ctrA=0 , ctrC=0while (ctrC<5) do{ C[ctrC]=A[ctrA] ctrC=ctrC+1ctrA=ctrA+1}while (ctrC<10) do{ C[ctrC]=B[ctrB] ctrC=ctrC+1 Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 14. Page 14 of 14ctrB=ctrB-1}ctrC=0while(ctrC<10) do{ print C[ctrC]ctrC= ctrC+1}END | Prepared By Sumit Kumar Gupta, PGT Computer Science

×