Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458.

Similar presentations


Presentation on theme: "Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458."— Presentation transcript:

1 Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

2 Selection Sort 6325122211 6325122211 25122263 void selectionSort(int[] a) { for (int i = 0; i < a.length - 1; i++) { int min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } if (i != min) { int swap = a[i]; a[i] = a[min]; a[min] = swap; } } } 1112252263 1112222563 i imin i i i, min 1112222563 i, min

3 Insertion Sort insertionSort(array A) begin for i := 1 to length[A] - 1 do begin value := A[ i ]; j := i - 1; while j >= 0 and A[ j ] > value do begin A[ j + 1] := A[ j ]; j := j - 1; end; A[ j + 1] := value; end; end; 6325122211 2563122211 1225632211 1222256311 12222563

4 Merge Sort – Partition Process 356233205724850 356233205724850 35623320 572 4850 356233205724850

5 Merge Sort – Merge Process 35 6233205724850

6 4020108060507301009070 [0][1][2][3][4][5][6][7][8][9][10] Starting at the beginning of the array, we look for the first element that is greater than the pivot. (tooBigIndex) Starting from the other end we look for the first value that is less than or euqal to the pivot. (tooSmallIndex) After finding the two out-of-place elements, exchange them. tooBigIndex++tooSmallIndex— Stop when tooBigIndex >= tooSmallIndex tooBigIndex tooSmallIndex Quicksort

7 4020108060507301009070 [0][1][2][3][4][5][6][7][8][9][10] tooBigIndex tooSmallIndex 4020103060507801009070 tooBigIndextooSmallIndex 4020103075060801009070 tooBigIndex tooSmallIndex

8 4020103075060801009070 tooBigIndextooSmallIndex 7201030405060801009070

9 Heapsort 21 42 22 27 23 45 35 1945 [0][1][2][3][4][5][6][7][8][9]

10 For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2] 452742212322351945 [0][1][2][3][4][5][6][7][8][9] 527422123223519445 [0][1][2][3][4][5][6][7][8][9] 422752123223519445 [0][1][2][3][4][5][6][7][8][9] 422735212322519445 [0][1][2][3][4][5][6][7][8][9]

11 422735212322519445 [0][1][2][3][4][5][6][7][8][9] 427352123225194245 [0][1][2][3][4][5][6][7][8][9] 352742123225194245 [0][1][2][3][4][5][6][7][8][9] 352722212345194245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]

12 352722212345194245 [0][1][2][3][4][5][6][7][8][9] 192722212345354245 [0][1][2][3][4][5][6][7][8][9] 271922212345354245 [0][1][2][3][4][5][6][7][8][9] 272322211945354245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]

13 272322211945354245 [0][1][2][3][4][5][6][7][8][9] 523222119427354245 [0][1][2][3][4][5][6][7][8][9] 235222119427354245 [0][1][2][3][4][5][6][7][8][9] 232122519427354245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]

14 232122519427354245 [0][1][2][3][4][5][6][7][8][9] 421225192327354245 [0][1][2][3][4][5][6][7][8][9] 222145192327354245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]

15 222145192327354245 [0][1][2][3][4][5][6][7][8][9] 192145222327354245 [0][1][2][3][4][5][6][7][8][9] 211945222327354245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]

16 211945222327354245 [0][1][2][3][4][5][6][7][8][9] 519421222327354245 [0][1][2][3][4][5][6][7][8][9] 195421222327354245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]

17 195421222327354245 [0][1][2][3][4][5][6][7][8][9] 451921222327354245 [0][1][2][3][4][5][6][7][8][9] 451921222327354245 [0][1][2][3][4][5][6][7][8][9] For an element in arr[i]: Parent[(i-1)/2] –Left child [2i+1] –Right child [2i+2]


Download ppt "Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458."

Similar presentations


Ads by Google