Quicksort: illustrated step-by-step walk through

58
Quicksort algorithm Illustrated walkthrough

description

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.

Transcript of Quicksort: illustrated step-by-step walk through

Page 1: Quicksort: illustrated step-by-step walk through

Quicksort algorithmIllustrated walkthrough

Page 2: Quicksort: illustrated step-by-step walk through

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

Page 3: Quicksort: illustrated step-by-step walk through

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]

Page 4: Quicksort: illustrated step-by-step walk through

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

Page 5: Quicksort: illustrated step-by-step walk through

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

Page 6: Quicksort: illustrated step-by-step walk through

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

Page 7: Quicksort: illustrated step-by-step walk through

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

Page 8: Quicksort: illustrated step-by-step walk through

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

Page 9: Quicksort: illustrated step-by-step walk through

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

Page 10: Quicksort: illustrated step-by-step walk through

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

Page 11: Quicksort: illustrated step-by-step walk through

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

Page 12: Quicksort: illustrated step-by-step walk through

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

Page 13: Quicksort: illustrated step-by-step walk through

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

Page 14: Quicksort: illustrated step-by-step walk through

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

Page 15: Quicksort: illustrated step-by-step walk through

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

Page 16: Quicksort: illustrated step-by-step walk through

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

Page 17: Quicksort: illustrated step-by-step walk through

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

Page 18: Quicksort: illustrated step-by-step walk through

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

Page 19: Quicksort: illustrated step-by-step walk through

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

Page 20: Quicksort: illustrated step-by-step walk through

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

Page 21: Quicksort: illustrated step-by-step walk through

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

Page 22: Quicksort: illustrated step-by-step walk through

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

Page 23: Quicksort: illustrated step-by-step walk through

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

Page 24: Quicksort: illustrated step-by-step walk through

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

Page 25: Quicksort: illustrated step-by-step walk through

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

Page 26: Quicksort: illustrated step-by-step walk through

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

Page 27: Quicksort: illustrated step-by-step walk through

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

Page 28: Quicksort: illustrated step-by-step walk through

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

Page 29: Quicksort: illustrated step-by-step walk through

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

Page 30: Quicksort: illustrated step-by-step walk through

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

Page 31: Quicksort: illustrated step-by-step walk through

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

Page 32: Quicksort: illustrated step-by-step walk through

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

Page 33: Quicksort: illustrated step-by-step walk through

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

Page 34: Quicksort: illustrated step-by-step walk through

Quicksort algorithm

Now we use Partition in the context of Quicksort

Page 35: Quicksort: illustrated step-by-step walk through

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]

Page 36: Quicksort: illustrated step-by-step walk through

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

Page 37: Quicksort: illustrated step-by-step walk through

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

Page 38: Quicksort: illustrated step-by-step walk through

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

Page 39: Quicksort: illustrated step-by-step walk through

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

Page 40: Quicksort: illustrated step-by-step walk through

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

Page 41: Quicksort: illustrated step-by-step walk through

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

Page 42: Quicksort: illustrated step-by-step walk through

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

Page 43: Quicksort: illustrated step-by-step walk through

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

Page 44: Quicksort: illustrated step-by-step walk through

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

Page 45: Quicksort: illustrated step-by-step walk through

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

Page 46: Quicksort: illustrated step-by-step walk through

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

Page 47: Quicksort: illustrated step-by-step walk through

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

Page 48: Quicksort: illustrated step-by-step walk through

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

Page 49: Quicksort: illustrated step-by-step walk through

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

Page 50: Quicksort: illustrated step-by-step walk through

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

Page 51: Quicksort: illustrated step-by-step walk through

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

Page 52: Quicksort: illustrated step-by-step walk through

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

Page 53: Quicksort: illustrated step-by-step walk through

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

Page 54: Quicksort: illustrated step-by-step walk through

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

Page 55: Quicksort: illustrated step-by-step walk through

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

Page 56: Quicksort: illustrated step-by-step walk through

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

Page 57: Quicksort: illustrated step-by-step walk through

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!

Page 58: Quicksort: illustrated step-by-step walk through

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.