bubble sort example 51428 15428 14528 14258 14258 14258 12458 12458 12458

17
Bubble Sort Example 5 1 4 2 8 1 5 4 2 8 1 4 5 2 8 1 4 2 5 8 1 4 2 5 8 1 4 2 5 8 1 2 4 5 8 1 2 4 5 8 1 2 4 5 8

Upload: bruce-goodman

Post on 18-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

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;

TRANSCRIPT

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

Bubble Sort Example

5 1 4 2 8

1 5 4 2 8

1 4 5 2 8

1 4 2 5 8

1 4 2 5 8

1 4 2 5 8

1 2 4 5 8

1 2 4 5 8

1 2 4 5 8

Page 2: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

Selection Sort63 25 12 22 11

63 25 12 22 11

11 25 12 22 63

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

11 12 25 22 63

11 12 22 25 63

i

i min

i min

i min

i, min

11 12 22 25 63i, min

Page 3: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

Insertion SortinsertionSort(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;

63 25 12 22 11

25 63 12 22 11

12 25 63 22 11

12 22 25 63 11

11 12 22 25 63

Page 4: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

Merge Sort – Partition Process

35 62 33 20 5 72 48 50

35 62 33 20 5 72 48 50

35 62 33 20 5 72 48 50

35 62 33 20 5 72 48 50

Page 5: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

Merge Sort – Merge Process

35 62 33 20 5 72 48 50

Page 6: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

40 20 10 80 60 50 7 30 100 90 70

[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 tooSmallIndexQuicksort

Page 7: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

40 20 10 80 60 50 7 30 100 90 70

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

tooBigIndex tooSmallIndex

40 20 10 30 60 50 7 80 100 90 70

tooBigIndex tooSmallIndex

40 20 10 30 7 50 60 80 100 90 70

tooBigIndex

tooSmallIndex

Page 8: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

40 20 10 30 7 50 60 80 100 90 70

tooBigIndextooSmallIndex

7 20 10 30 40 50 60 80 100 90 70

Page 9: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

Heapsort

21

42

22

27

23

45

35

19 4 5

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Page 10: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

• For an element in arr[i]: Parent[(i-1)/2]– Left child [2i+1]– Right child [2i+2]

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

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

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

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

Page 11: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

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

35 27 22 21 23 4 5 19 42 45[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]

Page 12: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

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

27 23 22 21 19 4 5 35 42 45[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]

Page 13: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

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

23 21 22 5 19 4 27 35 42 45[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]

Page 14: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

22 21 4 5 19 23 27 35 42 45[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]

Page 15: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

21 19 4 5 22 23 27 35 42 45[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]

Page 16: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

19 5 4 21 22 23 27 35 42 45[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]

Page 17: Bubble Sort Example 51428 15428 14528 14258 14258 14258 12458 12458 12458

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

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

4 5 19 21 22 23 27 35 42 45[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]