31
6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos January 13, 2014

New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

6331 - Algorithms, Spring 2014, CSE, OSULecture 3: Quicksort

Instructor: Anastasios Sidiropoulos

January 13, 2014

Page 2: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Sorting

Given an array of integers A[1 . . . n], rearrange its elements so that

A[1] ≤ A[2] ≤ . . . ≤ A[n].

Page 3: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Quicksort

Quicksort(A, p, r)if p < rq = Partition(A, p, r)Quicksort(A, p, q − 1)Quicksort(A, q + 1, r)

Page 4: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Partition

Partition(A, p, r)x = A[r ]i = p − 1for j = p to r − 1

if A[j ] ≤ xi = i + 1exchange A[i ] with A[j ]

exchange A[i + 1] with A[r ]return i + 1

What is the running time of the procedure Partition?

Page 5: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Partition

Partition(A, p, r)x = A[r ]i = p − 1for j = p to r − 1

if A[j ] ≤ xi = i + 1exchange A[i ] with A[j ]

exchange A[i + 1] with A[r ]return i + 1

What is the running time of the procedure Partition?

Page 6: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Invariants of Partition

I If p ≤ k ≤ i , then A[k] ≤ x .

I If i + 1 ≤ k ≤ j − 1, then A[k] > x .

I If k = r , then A[k] = x .

What does the procedure Partition do?

Page 7: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Invariants of Partition

I If p ≤ k ≤ i , then A[k] ≤ x .

I If i + 1 ≤ k ≤ j − 1, then A[k] > x .

I If k = r , then A[k] = x .

What does the procedure Partition do?

Page 8: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Lower bound on the worst-case performance of Quicksort?

Unbalanced partition.

T (n) ≥ T (n − 1) + T (0) + Θ(n)

= T (n − 1) + Θ(n)

= Ω(n2)

Page 9: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Lower bound on the worst-case performance of Quicksort?

Unbalanced partition.

T (n) ≥ T (n − 1) + T (0) + Θ(n)

= T (n − 1) + Θ(n)

= Ω(n2)

Page 10: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Lower bound on the worst-case performance of Quicksort?

Unbalanced partition.

T (n) ≥ T (n − 1) + T (0) + Θ(n)

= T (n − 1) + Θ(n)

= Ω(n2)

Page 11: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Upper bound on the worst-case performance of Quicksort?

T (n) = max0≤q≤n−1

T (q) + T (n − q − 1) + Θ(n)

We guess T (n) ≤ c · n2.

T (n) ≤ max0≤q≤n−1

(cq2 + c(n − q − 1)2) + Θ(n)

= c · max0≤q≤n−1

(q2 + (n − q − 1)2) + Θ(n)

We have max0≤q≤n−1(q2 + (n − q − 1)2) ≤ (n − 1)2.

T (n) ≤ cn2 − c(2n − 1) + Θ(n) ≤ cn2,

for c a large enough constant. Thus, T (n) = Θ(n2).

Page 12: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Upper bound on the worst-case performance of Quicksort?

T (n) = max0≤q≤n−1

T (q) + T (n − q − 1) + Θ(n)

We guess T (n) ≤ c · n2.

T (n) ≤ max0≤q≤n−1

(cq2 + c(n − q − 1)2) + Θ(n)

= c · max0≤q≤n−1

(q2 + (n − q − 1)2) + Θ(n)

We have max0≤q≤n−1(q2 + (n − q − 1)2) ≤ (n − 1)2.

T (n) ≤ cn2 − c(2n − 1) + Θ(n) ≤ cn2,

for c a large enough constant. Thus, T (n) = Θ(n2).

Page 13: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Upper bound on the worst-case performance of Quicksort?

T (n) = max0≤q≤n−1

T (q) + T (n − q − 1) + Θ(n)

We guess T (n) ≤ c · n2.

T (n) ≤ max0≤q≤n−1

(cq2 + c(n − q − 1)2) + Θ(n)

= c · max0≤q≤n−1

(q2 + (n − q − 1)2) + Θ(n)

We have max0≤q≤n−1(q2 + (n − q − 1)2) ≤ (n − 1)2.

T (n) ≤ cn2 − c(2n − 1) + Θ(n) ≤ cn2,

for c a large enough constant. Thus, T (n) = Θ(n2).

Page 14: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Worst-case performance of Quicksort

Upper bound on the worst-case performance of Quicksort?

T (n) = max0≤q≤n−1

T (q) + T (n − q − 1) + Θ(n)

We guess T (n) ≤ c · n2.

T (n) ≤ max0≤q≤n−1

(cq2 + c(n − q − 1)2) + Θ(n)

= c · max0≤q≤n−1

(q2 + (n − q − 1)2) + Θ(n)

We have max0≤q≤n−1(q2 + (n − q − 1)2) ≤ (n − 1)2.

T (n) ≤ cn2 − c(2n − 1) + Θ(n) ≤ cn2,

for c a large enough constant. Thus, T (n) = Θ(n2).

Page 15: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Performance of Quicksort

What happens when all the elements of A are equal?

For the rest of the lecture, we will assume that all elements aredistinct.

Page 16: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Performance of Quicksort

What happens when all the elements of A are equal?

For the rest of the lecture, we will assume that all elements aredistinct.

Page 17: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized Quicksort

Pick the pivot randomly.

Why would that make any difference?

Is this the same as average-case analysis?

Page 18: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized Quicksort

Pick the pivot randomly.

Why would that make any difference?

Is this the same as average-case analysis?

Page 19: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized Quicksort

Pick the pivot randomly.

Why would that make any difference?

Is this the same as average-case analysis?

Page 20: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized algorithms vs random input

The average running time on an algorithm for some distributionover inputs, analysis is not the same as the expected running timeof a randomized algorithm over an arbitrary input.

Examples?

Page 21: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized algorithms vs random input

The average running time on an algorithm for some distributionover inputs, analysis is not the same as the expected running timeof a randomized algorithm over an arbitrary input.

Examples?

Page 22: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized Quicksort

Randomized-Partition(A, p, r)i = Random(p, r)exchange A[i ] with A[i ]return Partition(A, p, r)

Randomized-Quicksort(A, p, r)if p < rq = Randomized-Partition(A, p, r)Randomized-Quicksort(A, p, q − 1)Randomized-Quicksort(A, q + 1, r)

What is the running time of the procedure Randomized-Partition?

Page 23: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized Quicksort

Randomized-Partition(A, p, r)i = Random(p, r)exchange A[i ] with A[i ]return Partition(A, p, r)

Randomized-Quicksort(A, p, r)if p < rq = Randomized-Partition(A, p, r)Randomized-Quicksort(A, p, q − 1)Randomized-Quicksort(A, q + 1, r)

What is the running time of the procedure Randomized-Partition?

Page 24: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Randomized Quicksort

Randomized-Partition(A, p, r)i = Random(p, r)exchange A[i ] with A[i ]return Partition(A, p, r)

Randomized-Quicksort(A, p, r)if p < rq = Randomized-Partition(A, p, r)Randomized-Quicksort(A, p, q − 1)Randomized-Quicksort(A, q + 1, r)

What is the running time of the procedure Randomized-Partition?

Page 25: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Expected running time of Randomized-Quicksort

Suppose the elements in A are z1, . . . , zn, with

z1 < z2 < . . . < zn.

Page 26: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Expected running time of Randomized-Quicksort

The running time is dominated by the number of comparisons.

Consider the indicator variable

Xij = Izi is compared to zj

The total number of comparisons is

X =n−1∑i=1

n∑j=i+1

Xij

Page 27: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Expected running time of Randomized-Quicksort

The running time is dominated by the number of comparisons.Consider the indicator variable

Xij = Izi is compared to zj

The total number of comparisons is

X =n−1∑i=1

n∑j=i+1

Xij

Page 28: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Expected running time of Randomized-Quicksort

The running time is dominated by the number of comparisons.Consider the indicator variable

Xij = Izi is compared to zj

The total number of comparisons is

X =n−1∑i=1

n∑j=i+1

Xij

Page 29: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Expected running time of Randomized-Quicksort

The expected running time is

E [X ] = E

n−1∑i=1

n∑j=i+1

Xij

=

n−1∑i=1

n∑j=i+1

E [Xij ]

=n−1∑i=1

n∑j=i+1

Przi is compared to zj

Page 30: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

The probability of a comparison

Suppose i < j .

Przi is compared to zj = Przi or zj is the first pivot in zi , . . . , zj≤ Przi is the first pivot in zi , . . . , zj

+ Przj is the first pivot in zi , . . . , zj

=1

j − i + 1+

1

j − 1 + 1

=2

j − i + 1

Page 31: New 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort · 2017. 8. 16. · 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 3: Quicksort Instructor: Anastasios Sidiropoulos

Expected running time of Randomized-Quicksort

The expected running time is

E [X ] =n−1∑i=1

n∑j=i+1

Przi is compared to zj

≤n−1∑i=1

n∑j=i+1

2

j − i + 1

≤n−1∑i=1

n−i∑k=1

2

k + 1

= O(n · log n)