Bubble sort An Animated Bubble Sort Example N
Bubble sort
An Animated Bubble Sort Example N 8 to_do 7 did_swap true index 98 23 45 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 1 did_swap false 98 23 45 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 1 did_swap false Swap 98 23 45 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 1 did_swap true Swap 23 98 45 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 2 did_swap 23 98 45 14 1 2 3 4 true 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 98 45 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 45 98 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 3 did_swap 23 45 98 14 1 2 3 4 true 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 98 14 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 14 98 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 4 did_swap 23 45 14 98 1 2 3 4 true 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 14 98 1 2 3 4 6 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 14 6 1 2 3 4 98 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 5 did_swap 23 45 14 6 1 2 3 4 true 98 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 14 6 1 2 3 4 98 67 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 14 6 1 2 3 4 67 98 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 6 did_swap 23 45 14 6 1 2 3 4 true 67 98 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 14 6 1 2 3 4 67 98 33 42 5 6 7 8
An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 14 6 1 2 3 4 67 33 98 42 5 6 7 8
An Animated Example N 8 to_do 7 index 7 did_swap 23 45 14 6 1 2 3 4 true 67 33 98 42 5 6 7 8
An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 14 6 1 2 3 4 67 33 98 42 5 6 7 8
An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
After First Pass of Outer Loop N 8 to_do 7 index 8 did_swap Finished first “Bubble Up” 23 45 14 6 1 true 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false No Swap 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false Swap 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap true Swap 23 14 45 6 1 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap 23 14 45 6 1 2 3 4 true 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 14 45 6 1 2 3 4 67 33 42 98 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 14 6 1 2 3 45 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 4 did_swap 23 14 6 1 2 3 true 45 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 4 did_swap true No Swap 23 14 6 1 2 3 45 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap 23 14 6 1 2 3 true 45 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 14 6 1 2 3 45 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 14 6 1 2 3 45 33 67 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap 23 14 6 1 2 3 true 45 33 67 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 14 6 1 2 3 45 33 67 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 14 6 1 2 3 45 33 42 67 98 4 5 6 7 8
After Second Pass of Outer Loop N 8 to_do 6 index 7 did_swap Finished second “Bubble Up” 23 14 6 1 true 2 3 45 33 42 67 98 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false 23 14 6 1 2 3 45 33 42 67 98 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false Swap 23 14 6 1 2 3 45 33 42 67 98 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap true Swap 14 23 6 1 2 3 45 33 42 67 98 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap 14 23 6 1 2 3 true 45 33 42 67 98 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 23 6 1 2 3 45 33 42 67 98 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 6 1 2 23 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 3 did_swap 14 6 1 2 true 23 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 3 did_swap true No Swap 14 6 1 2 23 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap 14 6 1 2 true 23 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 6 1 2 23 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 6 1 2 23 33 45 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap 14 6 1 2 true 23 33 45 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 6 1 2 23 33 45 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 6 1 2 23 33 42 45 67 98 3 4 5 6 7 8
After Third Pass of Outer Loop N 8 to_do 5 index 6 did_swap Finished third “Bubble Up” 14 6 1 true 2 23 33 42 45 67 98 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false 14 6 1 2 23 33 42 45 67 98 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false Swap 14 6 1 2 23 33 42 45 67 98 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap true Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 2 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 3 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 4 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1 did_swap true Finished fourth “Bubble Up” 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 did_swap false 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 1 did_swap false No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 did_swap false 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 2 did_swap false No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 did_swap false 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 3 did_swap false No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1 did_swap false Finished fifth “Bubble Up” 14 23 33 42 45 67 98 2 3 4 5 6 7 8
Finished “Early” N 8 to_do 3 index 4 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
Summary • “Bubble Up” algorithm will move largest value to its correct location (to the right) • Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs • We reduce the number of elements we compare each time one is correctly placed
Mergesort
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 98 23 Merge 45 14 6 67 33 42
98 23 45 14 98 23 23 Merge 45 14 6 67 33 42
98 23 45 14 98 23 23 98 Merge 45 14 6 67 33 42
98 23 45 14 98 23 23 98 45 14 6 67 33 42
98 23 45 14 23 98 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 Merge 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42
98 23 45 14 98 23 23 98 45 14 6 67 33 42 6 67 14 45 14 23 45 98 Merge 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 67 6 Merge 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 67 Merge 33 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 42 6 67 Merge
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 Merge
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 Merge
98 23 45 14 98 23 23 98 45 14 14 45 6 67 33 42 6 33 67 6 67 42 33 42 14 23 45 98 Merge
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 6 Merge
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 6 33 Merge
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 6 33 42 Merge
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 67 Merge
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 6 67 33 42 45 14 45 23 98 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 45 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 45 67 Merge 42
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 45 67 98 Merge 42
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 67 6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42 6 14 23 33 42 45 67 98
Summary • Divide the unsorted collection into two • Until the sub-arrays only contain one element • Then merge the sub-problem solutions together
Quick sort
Pick Pivot Element There a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 100
pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] too_big_index [3] [4] [5] 7 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] too_big_index [3] [4] [5] 7 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] too_big_index [3] [4] [5] 7 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] [3] [4] [5] too_big_index 7 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] [3] [4] [5] too_big_index 7 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] 7 [3] [4] [5] too_big_index 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind pivot_index = 0 40 [0] 20 10 80 60 50 [1] [2] 7 [3] [4] [5] too_big_index 30 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 60 50 [1] [2] 7 [3] [4] [5] too_big_index 80 100 [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index] pivot_index = 0 40 [0] 20 10 30 [1] [2] 7 50 60 80 100 [3] [4] [5] too_big_index [6] [7] [8] too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_ind 4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index] pivot_index = 4 7 [0] 20 10 30 40 50 60 80 100 [1] [2] [3] [4] [5] too_big_index [6] [7] [8] too_small_index
Partition Result 7 [0] 20 10 30 40 50 60 80 100 [1] [2] [3] [4] [5] <= data[pivot] [6] [7] [8] > data[pivot]
- Slides: 146