Quicksort




CS146

Chris Pollett

Mar 3, 2014

Outline

Introduction

Description of Quicksort

Partitioning the Array

Let's look at a first way we might go about implementing the partition procedure which rearranges the subarray `A[p..r]` in place:

PARTITION(A, p, r)
1 x = A[r]
2 i = p - 1
3 for j = p to r - 1
4     if A[j] <= x
5         i = i + 1
6         exchange A[i] with A[j]
7 exchange A[i+1] with A[r]
8 return i + 1

Example

Example of quicksort partition on 28713564

PARTITION loop invariant

Four ranges of values that appear in PARTITION procedure

Theorem. The partition function for quicksort satisfies the following loop invariant:

At the beginning of each iteration of the loop of lines 3-6, for any array index `k`

  1. If `p le k le i`, then `A[k] le x`
  2. If `i+1 le k le j-1` then `A[k] > x`
  3. If `k=r`, then `A[k] = x`

Proof. We need to show the three properties of loop invarants:

Initialization
Prior to the first iteration of the loop, `i = p - 1` and `j = p`. Because no values lie between `p` and `i` and no values lie between `i+1` and `j-1` the first two conditions of the loop invariant are trivially satisfied. The assignment in line 1 ensures the third condition.
Maintenance
There are two cases depending on the outcome of of the test in line 4. If `A[j] > x` the only action in the loop is to increment `j`. After `j` is incremented, condition 2 holds for `A[j-1]` and all other entries remain unchanged. (See case (a) of the image above). If `A[j] le x`, the loop increments `i`, swaps `A[i]` and `A[j]`, and then increments `j`. Because of the swap, we now have that `A[i] le x`, and condition 1. Similarly, we also have that `A[j-1] > x`, since the item that was swapped into `A[j-1]` is by the loop invariant greater than `x`. (See case (b) above.)
Termination
At termination, `j = r`. Therefore, every entry in the array is in one of the three sets described by the invariant, and we have partitioned the values in the array into three sets: those less than or equal to x, those greater than `x`, and a singleton set containing `x`.

In the termination setting, the exchange in line 7 puts `x` into the correct place in the partitioned array and the return statement gives the position of the partition in the array so it can be used by quicksort.

Since partition involves only a single loop and otherwise statements each involving constant work, its run time will be `Theta(n)`, where `n = r - p +1`.

Quiz (Sec 5)

Which of the following statements is true?

  1. If a heap has the max-heap property then `A[Pa\r\ent(i)] le A[i]` for all nodes `i` in the heap.
  2. The Master Theorem can be applied to the recurrence `T(n) = 4T(n/4) + sqrt(n)`.
  3. We used a probabilistic argument to show BUILD-MAX-HEAP(A) was an `Omega(n^2)` algorithm where `n = A.`length.

Quiz (Sec 6)

Which of the following statements is true?

  1. If `T(n) = 128 T(n/2) + 2^n`, then `T(n) = Theta(2^n)`.
  2. The master theorem can be applied to the recurrence `T(n) = (T(n/2))^2 + 1`.
  3. Inserting a new element into a heap require `Omega(n)` time where `n=A.`length.

Performance of Quicksort

Worst-case Partitioning

Best-case Partitioning