A step-by-step illustration of Quicksort to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation.
2. 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
3. [0]
[1]
[2]
[3]
[4]
[5]
12
7
14
9
10
11
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;
4. store
Index
i
begin
12
last
7
14
9
10
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;
11
5. i
store
Index
0
begin
12
last
7
14
9
10
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;
11
6. i
0
store
Index
0
begin
12
last
7
14
9
10
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;
11
8. i
store
Index
0
12 <= 11
is false
0
begin
12
last
7
14
9
10
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;
11
9. i
store
Index
1
0
begin
12
last
7
14
9
10
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;
11
10. i
store
Index
1
7 <= 11
is true
0
begin
12
last
7
14
9
10
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;
11
11. i
store
Index
1
0
begin
12
Swap
last
7
14
9
10
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;
11
12. i
store
Index
1
0
begin
7
Swap
last
12
14
9
10
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;
11
13. i
1
store
Index
1
begin
7
last
12
14
9
10
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;
11
14. i
store
Index
2
1
begin
7
last
12
14
9
10
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;
11
16. i
store
Index
2
14 <= 11
is fase
1
begin
7
last
12
14
9
10
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;
11
17. i
store
Index
3
1
begin
7
last
12
14
9
10
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;
11
19. i
store
Index
3
9 <= 11
is true
1
begin
7
last
12
14
9
10
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;
11
20. Swap
i
store
Index
3
1
begin
7
last
12
14
9
10
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;
11
21. Swap
i
store
Index
3
1
begin
7
last
9
14
12
10
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;
11
22. i
store
Index
3
2
begin
7
last
9
14
12
10
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;
11
23. i
store
Index
4
2
begin
7
last
9
14
12
10
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;
11
25. 10 <= 11
is true
store
Index
i
4
2
begin
7
last
9
14
12
10
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;
11
26. Swap
i
store
Index
4
2
begin
7
last
9
14
12
10
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;
11
27. Swap
i
store
Index
4
2
begin
7
last
9
10
12
14
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;
11
28. i
store
Index
4
3
begin
7
last
9
10
12
14
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;
11
29. i
store
Index
3
begin
7
5
last
9
10
12
14
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;
11
31. Swap
i
store
Index
3
begin
7
5
last
9
10
12
14
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;
11
32. Swap
i
store
Index
3
begin
7
5
last
9
10
11
14
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;
12
33. i
store
Index
3
begin
7
5
last
9
10
11
14
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;
12