Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.

Similar presentations


Presentation on theme: "Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be."— Presentation transcript:

1 Sorting an array bubble and selection sorts

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

3 Bubble sort

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

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

6 Bubble sort example Pass 2: Pass 2: B,D,C,A,Estart (from pass 1) B,D,C,A,Ecompare B,D,C,A,Edon’t swap B,D,C,A,Ecompare B,C,D,A,Eswap B,C,D,A,Ecompare B,C,A,D,Eswap 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: Pass 3: B,C,A,D,Estart (from pass 2) B,C,A,D,Ecompare B,C,A,D,Edon’t swap B,C,A,D,Ecompare B,A,C,D,Eswap Note that we don’t need to continue.

8 Bubble sort example Pass 4: Pass 4: B,A,C,D,Estart (from pass 3) B,A,C,D,Ecompare A,B,C,D,Eswap 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: 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. 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. 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,Astart D,B,E,C,Acompare B,D,E,C,Aswap B,D,E,C,Acompare B,D,E,C,Adon’t swap B,D,E,C,Acompare B,D,C,E,Aswap B,D,C,E,Acompare B,D,C,A,Eswap 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 (int i=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,Astart D,B,E,C,Acompare B,D,E,C,Aswap B,D,E,C,Acompare B,D,E,C,Adon’t swap B,D,E,C,Acompare B,D,C,E,Aswap B,D,C,E,Acompare B,D,C,A,Eswap 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,Astart D,B,E,C,Acompare B,D,E,C,Aswap B,D,E,C,Acompare B,D,E,C,Adon’t swap B,D,E,C,Acompare B,D,C,E,Aswap B,D,C,E,Acompare B,D,C,A,Eswap 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 Based on the idea of repeatedly finding the minimal elements. Based on the idea of repeatedly finding the minimal elements. How can we find the (single, most) minimal element in an array? 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 int whereSmallest = 0; for (int i=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: 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.    

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: 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: 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 ); }


Download ppt "Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be."

Similar presentations


Ads by Google