Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting array The bubblesort.

Similar presentations


Presentation on theme: "Sorting array The bubblesort."— Presentation transcript:

1 Sorting array The bubblesort

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

3 Bubblesort

4 Bubblesort algorithm Compare adjacent pairs of array elements
Swap if necessary (out of order) Repeat on next pair

5 Bubblesort example Given (D,B,E,C,A) We want (A,B,C,D,E) Pass 1:
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,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 Bubblesort 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,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 Bubblesort 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,A,C,D,E swap Note that we don’t need to continue.

8 Bubblesort 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 bubblesort 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 elements swaps it with the element at index i+1.

10 Coding the bubblesort public static void swap ( int start, int[] values ) { }

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

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

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

14 Coding the bubblesort 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 bubblesort 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,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap

16 Coding the bubblesort 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,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,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap

17 Coding the bubblesort 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,C,E,A swap B,D,C,E,A compare B,D,C,A,E swap

18 Coding the bubblesort 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 bubblesort 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 ); }

20 Selection Sort

21 Selection sort Based on the idea of repeated 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 int whereSmallest = 0; for (int i=1; i<A.length; i++) { if (A[i]<A[whereSmallest]) { whereSmallest = i; } out.writeln( “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 );


Download ppt "Sorting array The bubblesort."

Similar presentations


Ads by Google