# 1 The Partition Procedure 2 Algorithm 3 Worst-Case Behavior 4 Best-Case Behavior 5 Randomized QuickSort

• View
0

0

Embed Size (px)

Transcript

• QuickSort

Parosh Aziz Abdulla

Uppsala University

September 12, 2012

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 1 / 7

• 1 The Partition Procedure

2 Algorithm

3 Worst-Case Behavior

4 Best-Case Behavior

5 Randomized QuickSort

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 2 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• The Partition Procedure

Partition

Partition(A, p, r)

1 x ← A[r ] 2 i ← p − 1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i ]↔ A[j] 7 exchange A[i + 1]↔ A[r ] 8 return i + 1

2 9 8 1 3 6 7 5

i

j p r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 9 8 1 3 6 7 5

i p j r

2 1 8 9 3 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i j r

2 1 3 9 8 6 7 5

p i r

2 1 3 5 8 6 7 9

p i r

Partition(A, 1, 8)

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 3 / 7

• Algorithm

Quick Sort

QuickSort(A, p, r)

1 if p < r 2 then q ← Partition(A, p, r) 3 QuickSort(A, p, q − 1) 4 QuickSort(A, q + 1, r)

QuickSort(A, 1, 10)QuickSort(A, 1, 3)QuickSort(A, 1, 1)QuickSort(A, 3, 3)QuickSort(A, 5, 10)QuickSort(A, 5, 6)QuickSort(A, 5, 4)QuickSort(A, 6, 6)QuickSort(A, 8, 10)QuickSort(A, 8, 7)QuickSort(A, 9, 10)QuickSort(A, 9, 9)QuickSort(A, 11, 10)

Partition(A, 1, 10)Partition(A, 1, 3)Partition(A, 5, 10)Partition(A, 5, 6)Partition(A, 8, 10)Partition(A, 9, 10)(A,1,10)(A,1,10)

(A,1,3)(A,1,3)

(A,1,1)(A,1,1) (A,3,3)(A,3,3)

(A,5,10)(A,5,10)

(A,5,6)(A,5,6)

(A,5,4)(A,5,4) (A,6,6)(A,6,6)

(A,8,10)(A,8,10)

(A,8,7)(A,8,7) (A,9,10)(A,9,10)

(A,9,9)(A,9,9) (A,11,10)(A,11,10)

9

1

9

1

1

1

1

1

1

1

1

1

1

2

1

2

3

2

3

2

3

2

2

2

2

2

3

3

3

3

2

3

2

3

2

3

3

3

3

3

3

3

6

4

6

4

4

4

4

4

8

5

8

5

8

5

8

5

8

5

6

5

6

5

6

5

5

5

5

5

6

6

6

6

6

6

6

6

6

6

5

6

5

6

5

6

6

6

6

6

6

6

7

7

7

7

7

7

7

7

7

7

6

7

6

7

2

8

2

8

9

8

9

8

9

8

9

8

9

8

9

8

7

8

7

8

5

9

5

9

5

9

5

9

5

9

8

9

8

9

8

9

8

9

8

9

8

9

8

9

8

9

4

10

4

10

6

10

6

10

6

10

7

10

7

10

7

10

9

10

9

10

9

10

9

10

Parosh Aziz Abdulla (Uppsala University) QuickSort September 12, 2012 4 / 7

• Algorithm

Quick Sort

QuickSort(A, p, r)

1 if p < r 2 then q ← Partition(A, p, r) 3 QuickSort(A, p, q − 1) 4 QuickSort(A, q + 1, r)

QuickSort(A, 1, 10)QuickSo