SlideShare ist ein Scribd-Unternehmen logo
1 von 58
Downloaden Sie, um offline zu lesen
Quicksort algorithm
Illustrated 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
[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;
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
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
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
i

store
Index

0
0<5
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
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
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
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
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
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
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
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
i

store
Index

2
2<5
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
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
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
i

store
Index

3

3<5
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
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
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
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
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
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
4<5
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
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
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
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
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
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
4<5
is false

store
Index

i

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
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
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
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
Quicksort algorithm
Now we use Partition in the context of
Quicksort
pivot
Index

[0]

[1]

[2]

[3]

[4]

9

7

5

11

12

[5]

2

[6]

14

[7]

[8]

3

int pivotIndex = 0;
if (begin < last) {
pivotIndex = Partition(array, begin, last);
QuickSort(array, begin, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, last);
}
else {
return;
}

10

[9]

6
pivot
Index

0
9

7

5

11

12

2

14

3

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

6
0<9
is true
pivot
Index

0
9

7

5

11

12

2

14

3

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

6
Partition
0..9
pivot
Index

0
9

7

5

11

12

2

14

3

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

6
these are <= 6

these are > 6

pivot
Index

5

2

3

3

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #0

pivot
Index

5

2

3

3

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
pivot
Index
Call Stack #1
Call Stack #0

5

2

3

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
pivot
Index

0

5

2

3

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
0<2
is true

pivot
Index

0

5

2

3

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Partition
0..2
pivot
Index

0

5

2

3

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
pivot
Index

1

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #1
Call Stack #0

pivot
Index

1

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2

pivot
Index

Call Stack #1
Call Stack #0

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2
Call Stack #1
Call Stack #0

pivot
Index

0

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2

0<0
is false

Call Stack #1
Call Stack #0

pivot
Index

0

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2
Call Stack #1
Call Stack #0

pivot
Index

0

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #1
Call Stack #0

pivot
Index

1

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2

pivot
Index

Call Stack #1
Call Stack #0

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2
Call Stack #1
Call Stack #0

pivot
Index

2

0

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
0<0
is false

Call Stack #2
Call Stack #1
Call Stack #0

pivot
Index

2

0

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #2
Call Stack #1
Call Stack #0

pivot
Index

2

0

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
Call Stack #1
Call Stack #0

pivot
Index

1

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}
return (at the end of the function. Implicit ‘return’ statement

10

11
We are done with these elements!
Call Stack #0

pivot
Index

2

3

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11
pivot
Index
Call Stack #1
Call Stack #0

Walkthrough ends here.
The right hand side is also sorted as it
recursively calls Quicksort.

2

3

5

6

12

7

14

9

int pivotIndex = 0;
if (start < end) {
pivotIndex = Partition(array, start, end);
QuickSort(array, start, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, end);
}
else {
return;
}

10

11

Weitere ähnliche Inhalte

Was ist angesagt?

Power Point Tentang Peluang
Power Point Tentang PeluangPower Point Tentang Peluang
Power Point Tentang PeluangMatt Engky
 
Math induction principle (slides)
Math induction principle (slides)Math induction principle (slides)
Math induction principle (slides)IIUM
 
Integral tak tentu dan tertentu i
Integral tak tentu dan tertentu iIntegral tak tentu dan tertentu i
Integral tak tentu dan tertentu irafsanjanistrong
 
mencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di Cmencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di Ckir yy
 
2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)syarifaminullah2
 
Jenis jenis-pertidaksamaan
Jenis jenis-pertidaksamaanJenis jenis-pertidaksamaan
Jenis jenis-pertidaksamaanUjang Kasah
 
Sequence and Series in Discrete Structure
Sequence and Series in Discrete Structure Sequence and Series in Discrete Structure
Sequence and Series in Discrete Structure Zain Abid
 
Logika Matematika - Wahyu Fuadi, ST, M.IT
Logika Matematika - Wahyu Fuadi, ST, M.ITLogika Matematika - Wahyu Fuadi, ST, M.IT
Logika Matematika - Wahyu Fuadi, ST, M.ITsaid zulhelmi
 
Algebra 2. 9.19 Simplifying Square Roots
Algebra 2. 9.19 Simplifying Square RootsAlgebra 2. 9.19 Simplifying Square Roots
Algebra 2. 9.19 Simplifying Square Rootsdmatkeson21
 
Eliminasi substitusi
Eliminasi substitusiEliminasi substitusi
Eliminasi substitusiannisadera
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebraRonald Teo
 
Chapter 15au
Chapter 15auChapter 15au
Chapter 15auewalenta
 
Laporan Praktikum 3 DPK Operator
Laporan Praktikum 3 DPK OperatorLaporan Praktikum 3 DPK Operator
Laporan Praktikum 3 DPK OperatorHanifah Has
 
DISTRIBUSI HIPERGEOMETRIK
DISTRIBUSI HIPERGEOMETRIKDISTRIBUSI HIPERGEOMETRIK
DISTRIBUSI HIPERGEOMETRIKRarasenggar
 
Tutorial membangun linux from scratch dari awal
Tutorial membangun linux from scratch dari awalTutorial membangun linux from scratch dari awal
Tutorial membangun linux from scratch dari awalMuhammad Dzulfikri
 

Was ist angesagt? (20)

Power Point Tentang Peluang
Power Point Tentang PeluangPower Point Tentang Peluang
Power Point Tentang Peluang
 
Math induction principle (slides)
Math induction principle (slides)Math induction principle (slides)
Math induction principle (slides)
 
Integral tak tentu dan tertentu i
Integral tak tentu dan tertentu iIntegral tak tentu dan tertentu i
Integral tak tentu dan tertentu i
 
mencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di Cmencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di C
 
The Ideal Gas Law
The Ideal Gas LawThe Ideal Gas Law
The Ideal Gas Law
 
2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)
 
Jenis jenis-pertidaksamaan
Jenis jenis-pertidaksamaanJenis jenis-pertidaksamaan
Jenis jenis-pertidaksamaan
 
Pertemuan ke 14
Pertemuan ke  14Pertemuan ke  14
Pertemuan ke 14
 
Sequence and Series in Discrete Structure
Sequence and Series in Discrete Structure Sequence and Series in Discrete Structure
Sequence and Series in Discrete Structure
 
Logika Matematika - Wahyu Fuadi, ST, M.IT
Logika Matematika - Wahyu Fuadi, ST, M.ITLogika Matematika - Wahyu Fuadi, ST, M.IT
Logika Matematika - Wahyu Fuadi, ST, M.IT
 
Algebra 2. 9.19 Simplifying Square Roots
Algebra 2. 9.19 Simplifying Square RootsAlgebra 2. 9.19 Simplifying Square Roots
Algebra 2. 9.19 Simplifying Square Roots
 
Eliminasi substitusi
Eliminasi substitusiEliminasi substitusi
Eliminasi substitusi
 
Persamaan fungsi linier
Persamaan fungsi linierPersamaan fungsi linier
Persamaan fungsi linier
 
Algoritma Greedy
Algoritma GreedyAlgoritma Greedy
Algoritma Greedy
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebra
 
Truth table
Truth tableTruth table
Truth table
 
Chapter 15au
Chapter 15auChapter 15au
Chapter 15au
 
Laporan Praktikum 3 DPK Operator
Laporan Praktikum 3 DPK OperatorLaporan Praktikum 3 DPK Operator
Laporan Praktikum 3 DPK Operator
 
DISTRIBUSI HIPERGEOMETRIK
DISTRIBUSI HIPERGEOMETRIKDISTRIBUSI HIPERGEOMETRIK
DISTRIBUSI HIPERGEOMETRIK
 
Tutorial membangun linux from scratch dari awal
Tutorial membangun linux from scratch dari awalTutorial membangun linux from scratch dari awal
Tutorial membangun linux from scratch dari awal
 

Andere mochten auch

Andere mochten auch (8)

3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Safe laparoscopy
Safe laparoscopySafe laparoscopy
Safe laparoscopy
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 

Mehr von Yoshi Watanabe

Create images with AI models.pptx
Create images with AI models.pptxCreate images with AI models.pptx
Create images with AI models.pptxYoshi Watanabe
 
Git コンフリクト
Git コンフリクトGit コンフリクト
Git コンフリクトYoshi Watanabe
 
Find n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughFind n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughYoshi Watanabe
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthroughYoshi Watanabe
 
Binary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughBinary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughYoshi Watanabe
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughYoshi Watanabe
 

Mehr von Yoshi Watanabe (8)

Create images with AI models.pptx
Create images with AI models.pptxCreate images with AI models.pptx
Create images with AI models.pptx
 
Git フェッチ
Git フェッチGit フェッチ
Git フェッチ
 
Git リベース
Git リベースGit リベース
Git リベース
 
Git コンフリクト
Git コンフリクトGit コンフリクト
Git コンフリクト
 
Find n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughFind n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthrough
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthrough
 
Binary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughBinary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk through
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 

Quicksort: illustrated step-by-step walk through

  • 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
  • 7. i store Index 0 0<5 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
  • 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
  • 15. i store Index 2 2<5 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
  • 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
  • 18. i store Index 3 3<5 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
  • 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
  • 24. 4<5 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
  • 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
  • 30. 4<5 is false store Index i 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
  • 34. Quicksort algorithm Now we use Partition in the context of Quicksort
  • 35. pivot Index [0] [1] [2] [3] [4] 9 7 5 11 12 [5] 2 [6] 14 [7] [8] 3 int pivotIndex = 0; if (begin < last) { pivotIndex = Partition(array, begin, last); QuickSort(array, begin, pivotIndex - 1); QuickSort(array, pivotIndex + 1, last); } else { return; } 10 [9] 6
  • 36. pivot Index 0 9 7 5 11 12 2 14 3 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 6
  • 37. 0<9 is true pivot Index 0 9 7 5 11 12 2 14 3 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 6
  • 38. Partition 0..9 pivot Index 0 9 7 5 11 12 2 14 3 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 6
  • 39. these are <= 6 these are > 6 pivot Index 5 2 3 3 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 40. Call Stack #0 pivot Index 5 2 3 3 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 41. pivot Index Call Stack #1 Call Stack #0 5 2 3 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 42. pivot Index 0 5 2 3 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 43. 0<2 is true pivot Index 0 5 2 3 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 44. Partition 0..2 pivot Index 0 5 2 3 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 45. pivot Index 1 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 46. Call Stack #1 Call Stack #0 pivot Index 1 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 47. Call Stack #2 pivot Index Call Stack #1 Call Stack #0 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 48. Call Stack #2 Call Stack #1 Call Stack #0 pivot Index 0 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 49. Call Stack #2 0<0 is false Call Stack #1 Call Stack #0 pivot Index 0 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 50. Call Stack #2 Call Stack #1 Call Stack #0 pivot Index 0 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 51. Call Stack #1 Call Stack #0 pivot Index 1 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 52. Call Stack #2 pivot Index Call Stack #1 Call Stack #0 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 53. Call Stack #2 Call Stack #1 Call Stack #0 pivot Index 2 0 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 54. 0<0 is false Call Stack #2 Call Stack #1 Call Stack #0 pivot Index 2 0 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 55. Call Stack #2 Call Stack #1 Call Stack #0 pivot Index 2 0 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 56. Call Stack #1 Call Stack #0 pivot Index 1 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } return (at the end of the function. Implicit ‘return’ statement 10 11
  • 57. We are done with these elements! Call Stack #0 pivot Index 2 3 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11
  • 58. pivot Index Call Stack #1 Call Stack #0 Walkthrough ends here. The right hand side is also sorted as it recursively calls Quicksort. 2 3 5 6 12 7 14 9 int pivotIndex = 0; if (start < end) { pivotIndex = Partition(array, start, end); QuickSort(array, start, pivotIndex - 1); QuickSort(array, pivotIndex + 1, end); } else { return; } 10 11