0% found this document useful (0 votes)
16 views26 pages

Lec 4

The document discusses the Quick Sort algorithm, introduced by C.A.R. Hoare in 1962, which is a divide-and-conquer sorting method that sorts in place. It details the partitioning process, pseudocode for implementation, and the analysis of its worst-case performance, particularly when the input is sorted or reverse sorted. The worst-case time complexity is Θ(n^2), while practical applications can achieve better performance with optimized partitioning techniques.

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views26 pages

Lec 4

The document discusses the Quick Sort algorithm, introduced by C.A.R. Hoare in 1962, which is a divide-and-conquer sorting method that sorts in place. It details the partitioning process, pseudocode for implementation, and the analysis of its worst-case performance, particularly when the input is sorted or reverse sorted. The worst-case time complexity is Θ(n^2), while practical applications can achieve better performance with optimized partitioning techniques.

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSE-209

Algorithms-I

Lecture 4

Sorting: Quick Sort

Md. Rafsan Jani


Assistant Professor
Department of Computer Science and Engineering
Jahangirnagar University
Quicksort

•Proposed by C.A.R. Hoare in 1962.


•Divide-and-conquer algorithm.
•Sorts “in place” (like insertion sort, but not
like merge sort).
•Very practical (with tuning).

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.2
Divide and conquer

Quicksort an n-element array:


1. Divide: Partition the array into two subarrays
around a pivot x such that elements in lower
subarray ≤ x ≤ elements in upper subarray.
≤≤ x xx ≥≥ x
2. Conquer:x Recursively sort thex two subarrays.
3. Combine: Trivial.
Key: Linear-time partitioning subroutine.
September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.3
Partitioning subroutine

PARTITION(A, p, q) ⊳ A[ p . . q]
x ← A[ p] ⊳ pivot = A[ p]
i←p R u n n in
= ROu (nn n) i
for j ← p + 1 to q = tO ( ne)
g i m
fn og rt ni m e
do if A[ j] ≤ x f or n
then i ← i + 1 e le m e
el e m e
exchange A[i] ↔ A[
n ts .
j] exchange A[ p] ↔ A[i] nt s.
return i
xx ≤≤ x ≥≥ x ??
Invariant:
pi j x x q
September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.4
Example of partitioning

66 1010 13
13 55 88 33 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.5
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.6
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.7
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.8
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.9
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.10
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
66 55 33 10
10 88 13
13 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.11
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
66 55 33 10
10 88 13
13 22 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.12
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
66 55 33 10
10 88 13
13 22 11
11
66 55 33 22 88 13
13 10
10 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.13
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
66 55 33 10
10 88 13
13 22 11
11
66 55 33 22 88 13
13 10
10 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.14
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
66 55 33 10
10 88 13
13 22 11
11
66 55 33 22 88 13
13 10
10 11
11
i j

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.15
Example of partitioning

66 10
10 13
13 55 88 33 22 11
11
66 55 13
13 10
10 88 33 22 11
11
66 55 33 10
10 88 13
13 22 11
11
66 55 33 22 88 13
13 10
10 11
11
22 55 33 66 88 13
13 10
10 11
11
i
September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.16
Pseudocode for quicksort

QUICKSORT(A, p, r)
if p < r
then q ← PARTITION(A, p, r)
QUICKSORT(A, p, q–1)
QUICKSORT(A, q+1, r)

Initial call: QUICKSORT(A, 1, n)

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.17
Analysis of quicksort

•Assume all input elements are distinct.


•In practice, there are better partitioning
algorithms for when duplicate input
elements may exist.
•Let T(n) = worst-case running time on
an array of n elements.

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.18
Worst-case of quicksort

•Input sorted or reverse sorted.


•Partition around min or max element.
•One side of partition always has no elements.
T (n) = T (0) + T (n −1) + Θ(n)
= Θ(1) + T (n −1) + Θ(n)
= T (n −1) + Θ(n)
2 (arithmetic series)
= Θ(n )

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.19
Worst-case recursion tree
T(n) = T(0) + T(n–1) + cn

September 21,
2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.20
Worst-case recursion tree
T(n) = T(0) + T(n–1) + cn

T(n)

September 21,
2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.21
Worst-case recursion
tree
T(n) = T(0) + T(n–1) + cn
cn
T(0) T(n–1)

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.22
Worst-case recursion
tree
T(n) = T(0) + T(n–1) + cn
cn
T(0) c(n–1)
T(0) T(n–2)

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.23
Worst-case recursion
tree
T(n) = T(0) + T(n–1) + cn
cn
c(n–1)
T(0)
T(0) c(n–2)

T(0) O

Θ(1)

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.24
Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn


cn ⎞
⎜⎛n ⎟
T(0) c(n–1) Θ⎝ k ⎠ = Θ(n2
T(0) c(n–2)

k
) =1
T(0) O

Θ(1)

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.25
Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn


cn ⎞
⎜⎛n ⎟
Θ(1) c(n–1) Θ⎝ k ⎠ = Θ(n2
Θ(1) c(n–2)

k
h=n ) =1
T(n) = Θ(n) + Θ(n2)
Θ(1) O = Θ(n2)

Θ(1)

September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.26

You might also like