Quicksort algorithmIllustrated walkthrough
Partition function
This function does the most of the heavy lifting, so we look at it first, then see it in the context of Quicksort algorithm
12 7 14 9 10 11
[0]
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
[1] [2] [3] [4] [5]
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
istore Index
12 7 14 9 10 11
begin last
0
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
store Index
i
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
0i
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
0i
0 < 5is true
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
0i
12 <= 11is false
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
1i
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
1i
7 <= 11is true
12 7 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
1i
Swap
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
0store Index
1i
Swap
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
1i
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
2i
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
2i
2 < 5is true
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
2i
14 <= 11is fase
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
3i
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
3i3 < 5is true
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
3i9 <= 11is true
7 12 14 9 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
3i
Swap
7 9 14 12 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
1store Index
3i
Swap
7 9 14 12 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
2store Index
3i
7 9 14 12 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
2store Index
4i
7 9 14 12 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
2store Index
4i4 < 5is true
7 9 14 12 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
2store Index
4i10 <= 11
is true
7 9 14 12 10 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
2store Index
4i
Swap
7 9 10 12 14 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
2store Index
4i
Swap
7 9 10 12 14 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
3store Index
4i
7 9 10 12 14 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
3store Index
5i
7 9 10 12 14 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
3store Index
5i
4 < 5is false
7 9 10 12 14 11
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
3store Index
5i
Swap
7 9 10 11 14 12
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
3store Index
5i
Swap
7 9 10 11 14 12
begin last
int storeIndex = begin;for (int i = begin; i < last; i++) { if (array[i] <= array[last]) { Swap(array, i, storeIndex); storeIndex = storeIndex + 1; }}Swap(array, storeIndex, last);return storeIndex;
3store Index
5i
Quicksort algorithm
Now we use Partition in the context of Quicksort
9 7 5 11 12 2 14 3 10 6
int pivotIndex = 0;if (begin < last) { pivotIndex = Partition(array, begin, last); QuickSort(array, begin, pivotIndex - 1); QuickSort(array, pivotIndex + 1, last); }else { return;}
pivot Index
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
9 7 5 11 12 2 14 3 10 6
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
0pivot Index
9 7 5 11 12 2 14 3 10 6
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
0pivot Index
0 < 9is true
9 7 5 11 12 2 14 3 10 6
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
0pivot Index
Partition0..9
5 2 3 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
3pivot Index
these are <= 6 these are > 6
5 2 3 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
3pivot Index
Call Stack #0
5 2 3 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
pivot Index
Call Stack #0
Call Stack #1
5 2 3 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
0pivot Index
5 2 3 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
0pivot Index
0 < 2is true
5 2 3 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
0pivot Index
Partition0..2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
1pivot Index
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
1pivot Index
Call Stack #0
Call Stack #1
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
pivot Index
Call Stack #2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
0pivot Index
Call Stack #2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
0pivot Index
Call Stack #20 < 0
is false
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
0pivot Index
Call Stack #2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
1pivot Index
Call Stack #0
Call Stack #1
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
pivot Index
Call Stack #2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
0pivot Index
Call Stack #2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
0pivot Index
Call Stack #20 < 0is false
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
Call Stack #0
Call Stack #1
0pivot Index
Call Stack #2
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
1pivot Index
Call Stack #0
Call Stack #1
return (at the end of the function. Implicit ‘return’ statement
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
3pivot Index
Call Stack #0
We are done with these elements!
2 3 5 6 12 7 14 9 10 11
int pivotIndex = 0;if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); }else { return;}
pivot Index
Call Stack #0
Call Stack #1
Walkthrough ends here.The right hand side is also sorted as it recursively calls Quicksort.
Top Related