Bubble Sort Example
Bubble Sort Example
5 1 4 2 8 1 4 2 5 8
1 5 4 2 8 1 2 4 5 8
1 4 5 2 8 1 2 4 5 8
1 4 2 5 8 1 2 4 5 8
1 4 2 5 8
Selection Sort
i
void selectionSort(int[] a) {
63 25 12 22 11 for (int i = 0; i < a.length - 1; i++) {
int min = i;
i min
for (int j = i + 1; j < a.length; j++) {
63 25 12 22 11 if (a[j] < a[min]) {
min = j;
i min }
11 25 12 22 63 }
if (i != min) {
i min int swap = a[i];
a[i] = a[min];
11 12 25 22 63 a[min] = swap;
i, min }
}
11 12 22 25 63 }
i, min
11 12 22 25 63
Insertion Sort
63 25 12 22 11 insertionSort(array A)
begin
for i := 1 to length[A] - 1 do
begin
25 63 12 22 11 value := A[ i ];
j := i - 1;
while j >= 0 and A[ j ] > value do
12 25 63 22 11 begin
A[ j + 1] := A[ j ];
j := j - 1;
end;
12 22 25 63 11 A[ j + 1] := value;
end;
end;
11 12 22 25 63
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
Merge Sort – Merge Process
35 62 33 20 5 72 48 50
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
40 20 10 80 60 50 7 30 100 90 70
tooBigIndex
Quicksort tooSmallIndex
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—
40 20 10 80 60 50 7 30 100 90 70
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
40 20 10 30 7 50 60 80 100 90 70
tooSmallIndex tooBigIndex
7 20 10 30 40 50 60 80 100 90 70
Heapsort
45
27 42
21 23 22 35
19 4 5
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
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]
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]
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]
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]
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]
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]
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]
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]