Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

11. Arrays

Data structure array

  • Login to see the comments

11. Arrays

  1. 1. Arrays By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and DataJava and Data StructuresStructures
  2. 2. Array • An array is a collection of homogeneous type of data elements. • An array is consisting of a collection of elements . • Operation Performed On Array: 1. Traversing 2. Search 3. Insertion 4. Deletion 5. Sorting 6. Merging Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 1 2 3 4 5 Representation of array
  3. 3. Linear Arrays • A linear array is the list of finite number ‘N’ of homogeneous data elements (i.e. data elements of same type) • Length = UB – LB + 1 • Array A may be denoted by, – A1, A2, A3, … AN - Subscript Notation – A(1), A(2), A(3), …., A(N) – Fortran, PL/I, BASIC – A[1], A[2], A[3],..,A[N] – PASCAL, C, C++, Java Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  4. 4. Traversing • This algorithm traverses a linear array A with lower bound LB and upper bound UB. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). //Using for loop for k := LB to UB Process -> LA [k]; //Using while loop k := LB while(k <= UB) { Process -> LA[k]; k:= k + 1; }
  5. 5. Inserting 22 23 26 28 29 31 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). LA[1] LA[2] LA[3] LA[4] LA[5] LA[6] LA[7] 22 23 25 26 28 29 31 Algorithm Insert(LA, N, K, ITEM) { J := N; //Initialize counter while(J >= K) { LA[J + 1] := LA [J]; // Move one elements downward J := J - 1; // Decrease counter by 1 } LA[K] := ITEM // Insert element N := N + 1; // Reset N }
  6. 6. Deleting Here LA is a linear array with N elements. LOC is the location where ITEM is to be deleted. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm Delete(LA, N, LOC, ITEM) { ITEM := LA[LOC] // Assign the elements to be deleted for J := LOC to N do J := J + 1 { LA[J] := LA [J + 1]; // Move Jth element upwards } N := N - 1; // Reset N }
  7. 7. Searching Algorithms • Linear Search – A linear search sequentially moves through your collection (or data structure) looking for a matching value. – Worst case performance scenario for a linear search is that it needs to loop through the entire collection; either because the item is the last one, or because the item isn't found. – In other words, if you have N items in your collection, the worst case scenario to find an item is N iterations. This is known as O(N) using the Big O Notation. – Linear searches don't require the collection to be sorted. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  8. 8. Linear Search Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm LinearSearch (LA, N, ITEM) { for J := 1 to N do J := J + 1 { if(ITEM = LA [j]) then { write (ITEM +" found at location " +J); Return; } } if(J > N) then { write (ITEM +" does not exist."); } } 55 88 66 77 99 11 6 J = 1 J = 2 J = 3 = 6= 6 = 6= 6 = 6= 6 6 found at location 3
  9. 9. Searching Algorithms • Binary Search – Binary search relies on a divide and conquer strategy to find a value within an already-sorted collection. – The algorithm is deceptively simple. – Binary search requires a sorted collection. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  10. 10. Binary Search ITEM = 40 BEG = 1, END = 13 BEG = 1, END = 6 40 > 30 BEG = 4, END = 6 40 = 40 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 11 22 30 33 40 44 55 60 66 77 80 88 99 11 22 30 33 40 44 55 60 66 77 80 88 99 11 22 30 33 40 44 55 60 66 77 80 88 99 40 < 55
  11. 11. Binary Search Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm Binary (DATA, LB, UB, ITEM, LOC) { BEG := LB; END := UB; MID := INT((BEG + END)/2); while(BEG <= END && DATA [MID] != ITEM) { if (ITEM < DATA [MID]) then END := MID - 1; else BEG := MID + 1; MID := INT((BEG + END)/2); } if(DATA [MID] = ITEM) then LOC := MID; else LOC := NULL; }
  12. 12. Sorting • Bubble Sort • Insertion Sort • Selection Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  13. 13. Bubble Sort Suppose the following numbers are stored in an array A. We apply bubble sort to the array A. Pass 1 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 32 51 27 85 66 23 13 32 51 27 85 66 23 13 32 51 27 85 66 23 13 32 27 51 85 66 23 13 32 27 51 85 66 23 13 32 27 51 66 85 23 13 32 27 51 66 23 85 13 32 27 51 66 23 13 85 32 < 51 51 > 27 51 < 85 85 > 66 85 < 23 85 >13
  14. 14. Bubble Sort Suppose the following numbers are stored in an array A. We apply bubble sort to the array A. Pass 2 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 32 51 27 85 66 23 13 32 27 51 66 23 13 85 27 32 51 66 23 13 85 27 32 51 66 23 13 85 27 32 51 66 23 13 85 27 32 51 23 66 13 85 27 32 51 23 13 66 85 27 32 51 23 13 66 85 32 > 27 32 < 51 51 < 66 66 > 23 66 > 13 66 < 85
  15. 15. Bubble Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm Bubble (DATA, N) { for K : = 1 to N -1 do K := K + 1 { PTR : = 1; While(PTR <= N - K) { if(DATA [PTR] > DATA [PTR +1]) then Interchange (DATA [PTR] and DATA [PTR +1]) PTR := PTR +1; } } }
  16. 16. Insertion Sort • Suppose an array A with N elements, the insertion sort algorithm scans A from 1 to N, inserting each element A [K] into its proper position in the previously sorted sub-array A[1], A [2] , …, A[K-1]. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 77 33 44 11 88 22 66 55 77 33 44 11 88 22 66 55 33 77 44 11 88 22 66 55 33 44 77 11 88 22 66 55 11 33 44 77 88 22 66 55 11 33 44 77 88 22 66 55 11 22 33 44 77 88 66 55 11 22 33 44 66 77 88 55 11 22 33 44 55 66 77 88 Pass K = 1 K = 2 K = 3 K = 4 K = 5 K = 6 K = 7 K = 8 Sorted 1 2 3 4 5 6 7 8
  17. 17. Insertion Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm INSERTION (DATA, N) { for K:= 1 to N do K := K + 1 { TEMP := DATA [K]; PTR := K - 1; while (PTR >= 0 && TEMP < DATA [PTR]) { DATA [PTR + 1] := DATA [PTR]; PTR = PTR - 1; } DATA [PTR + 1]:= TEMP; } }
  18. 18. Selection Sort • Selection sort algorithm for sorting A works as follows: – First find the smallest element in the list and put it in the first position. – Then find the second smallest element in the list and put it in the second position. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  19. 19. Selection Sort Suppose an array A with 8 elements as follows: 77, 33, 44, 11, 88, 22, 66, 55. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 77 33 44 11 88 22 66 55 11 33 44 77 88 22 66 55 11 22 44 77 88 33 66 55 11 22 33 77 88 44 66 55 11 22 33 44 88 77 66 55 11 22 33 44 55 77 66 88 11 22 33 44 55 66 77 88 11 22 33 44 55 66 77 88 Pass K = 1 K = 2 K = 3 K = 4 K = 5 K = 6 K = 7 1 2 3 4 5 6 7 8 LOC = 4 LOC = 6 LOC = 6 LOC = 6 LOC = 8 LOC = 7 LOC = 7 Sorted
  20. 20. Selection Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm SELECTION (A, N) { for J := 1 to N do J := J + 1 { MINI(A, J, N, LOC); [Interchange A[J] and A[LOC]]; TEMP := A[J]; A[J] := A[LOC]; A[LOC] := TEMP; } } MINI(A, K, N, LOC) { MIN := A[K] and LOC := K; for J := K + 1 to N do J := J + 1 { if (MIN > A [J]) then { MIN := A [J]; LOC := J; } } Return LOC; }
  21. 21. Q & A

×