1 / 28

Sorting an array

Sorting an array. bubble and selection sorts. Sorting. An arrangement or permutation of data May be either: ascending (non decreasing) descending (non increasing). Bubble sort. Bubble sort algorithm. Compare adjacent pairs of array elements Swap if necessary (out of order)

zuwena
Télécharger la présentation

Sorting an array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting an array bubble and selection sorts

  2. Sorting • An arrangement or permutation of data • May be either: • ascending (non decreasing) • descending (non increasing)

  3. Bubble sort

  4. Bubble sort algorithm • Compare adjacent pairs of array elements • Swap if necessary (out of order) • Repeat on next pair

  5. Bubble sort example • Given (D,B,E,C,A) • We want (A,B,C,D,E) • Smallest to largest; ascending; non decreasing • Pass 1: D,B,E,C,A start D,B,E,C,A compare B,D,E,C,A swap B,D,E,C,A compare B,D,E,C,A don’t swap B,D,E,C,A compare B,D,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap Note that at the end of pass 1, the last element (E above) is the largest.

  6. Bubble sort example • Pass 2: B,D,C,A,E start (from pass 1) B,D,C,A,E compare B,D,C,A,E don’t swap B,D,C,A,E compare B,C,D,A,E swap B,C,D,A,E compare B,C,A,D,E swap Note that we don’t need to continue and compare D and E because E was the overall maximum. Further note that D is the max of the sub array considered in pass 2.

  7. Bubble sort example • Pass 3: B,C,A,D,E start (from pass 2) B,C,A,D,E compare B,C,A,D,E don’t swap B,C,A,D,E compare B,A,C,D,E swap Note that we don’t need to continue.

  8. Bubble sort example • Pass 4: B,A,C,D,E start (from pass 3) B,A,C,D,E compare A,B,C,D,E swap Done! In general, if the length of the list is N, we need to make N-1 passes.

  9. Coding the bubble sort • First, we need the swap operation: 1,2,4,3,5 becomes 1,2,3,4,5 • Write a function that given the index i of an array element swaps it with the element at index i+1.

  10. Coding the bubble sort public static void swap ( int start, int[] values ) { … }

  11. Coding the bubble sort public static void swap ( int start, int[] values ) { int temp = values[ start ]; … }

  12. Coding the bubble sort public static void swap ( int start, int[] values ) { int temp = values[ start ]; values[ start ] = values[ start + 1 ]; … }

  13. Coding the bubble sort public static void swap ( int start, int[] values ) { int temp = values[ start ]; values[ start ] = values[ start + 1 ]; values[ start + 1 ] = temp; }

  14. Coding the bubble sort • Now we need a function to call swap when that operation is necessary. public static void bubblesort ( int[] values ) { … } Recall: In general, if the length of the list is N, we need to make N-1 passes.

  15. Coding the bubble sort Recall: In general, if the length of the list is N, we need to make N-1 passes. public static void bubblesort ( int[] values ) { //perform one pass through the array for (int i=0; i<values.length; i++) { … } } Recall from our example: Pass 1: D,B,E,C,A start D,B,E,C,A compare B,D,E,C,A swap B,D,E,C,A compare B,D,E,C,A don’t swap B,D,E,C,A compare B,D,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap Unlike the selection sort where we most avoid repeatedly starting at the beginning in the inner loop each time, the bubble sort can repeatedly process the entire array in the inner loop. Later, we will avoid this for reasons of efficiency.

  16. Coding the bubble sort public static void bubblesort ( int[] values ) { //perform one pass through the array for (inti=0; i<values.length; i++) { for (int j=0; j<values.length-1; j++) { // swap if necessary … } } } Recall from our example: Pass 1: D,B,E,C,A start D,B,E,C,A compare B,D,E,C,A swap B,D,E,C,A compare B,D,E,C,A don’t swap B,D,E,C,A compare B,D,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap Note that we need to shorten up by one for the last pair.

  17. Coding the bubble sort public static void bubblesort ( int[] values ) { //perform one pass through the array for (int i=0; i<values.length; i++) { for (int j=0; j<values.length-1; j++) { // swap if necessary if (values[j] > values[j+1]) { swap( j, values ); } } } } Recall from our example: Pass 1: D,B,E,C,A start D,B,E,C,A compare B,D,E,C,A swap B,D,E,C,A compare B,D,E,C,A don’t swap B,D,E,C,A compare B,D,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap Note that we need to shorten up by one for the last pair. See why that was necessary?

  18. Coding the bubble sort But wait! The inner loop (j) always goes from start to end but we said that each pass ensures that the last element in the sub array is maximal and we don’t need to check so far each time. public static void bubblesort ( int[] values ) { //perform one pass through the array for (int i=0; i<values.length; i++) { for (int j=0; j<values.length-1; j++) { // swap if necessary if (values[j] > values[j+1]) { swap( j, values ); } } } }

  19. Coding the bubble sort But wait! The inner loop (j) always goes from start to end but we said that each pass ensures that the last element in the sub array is maximal and we don’t need to check so far each time. public static void bubblesort ( int[] values ) { //perform one pass through the array for (int i=0; i<values.length; i++) { for (int j=0; j<values.length-1-i; j++) { // swap if necessary if (values[j] > values[j+1]) { swap( j, values ); } } } } Unlike the selection sort where we most avoid repeatedly starting at the beginning in the inner loop each time, the bubble sort can repeatedly process the entire array in the inner loop. Here, we avoid this for reasons of efficiency.

  20. Selection sort

  21. Selection sort • Based on the idea of repeatedly finding the minimal elements. • How can we find the (single, most) minimal element in an array?

  22. Selection sort How can we find the (single, most) minimal element in an array? … //let 0 be the location of the smallest element so far intwhereSmallest = 0; for (inti=1; i<A.length; i++) { if (A[i]<A[whereSmallest]) { whereSmallest = i; } } System.out.println( "the smallest is " + A[whereSmallest] + " which was located at position " + whereSmallest + "." );

  23. Selection sort • Idea: • Find the smallest in A[0]..A[ A.length-1 ]. • Put that in A[0]. • Then find the smallest in A[1]..A[ A.length-1 ]. • Put that in A[1]. • … • But first, let’s develop a swapPairs function that swaps a pair of elements denoted by a and b in some array, A. • <-1, 2, 4, -5, 12>  <-5, 2, 4, -1, 12>

  24. Selection sort public static void swapPair ( … ) { … } What do we need in here to do the job (function parameters)?

  25. Selection sort public static void swapPair ( int[] A, int a, int b ) { … } What do we need in here to do the job (function parameters)?

  26. Selection sort public static void swapPair ( int[] A, int a, int b ) { int temp = A[a]; A[a] = A[b]; A[b] = temp; }

  27. Selection sort • Idea: • Find the smallest in A[0]..A[ A.length-1 ]. • Put that in A[0]. • Then find the smallest in A[1]..A[ A.length-1 ]. • Put that in A[1]. … //let 0 be the location of the smallest element so far int whereSmallest = 0; for (int i=1; i<A.length; i++) { if (A[i]<A[whereSmallest]) { whereSmallest = i; } } swapPairs( A, 0, whereSmallest );

  28. Selection sort • Idea: • Find the smallest in A[0]..A[ A.length-1 ]. • Put that in A[0]. • Then find the smallest in A[1]..A[ A.length-1 ]. • Put that in A[1]. … for (int j=0; j<A.length; j++) { //let j be the location of the smallest element so far int whereSmallest = j; for (int i=j+1; i<A.length; i++) { if (A[i]<A[whereSmallest]) { whereSmallest = i; } } swap( A, j, whereSmallest ); }

More Related