# Lecture 22 - Probabilistic Analysis Flashcards

What’s the worst case partition of quick sort? In words first, then derive it from T(n).

Split off a single element at each level. T(n) = T(n-1) + T(0) + PartitionTime(n) = T(n – 1) + Q(n) = sum(k=1 to n) Q(n) = Q (n^2)

What’s the best case partition of quick sort? In words first, then derive it from T(n).

Each subproblem size <= n/2.

T(n) <= 2T(n/2) + PartitionTime(n)

= Q(nlgn)

What types of lists is quicksort least efficient on? Give 2 fixes to this problem

- Quicksort is not very efficient on small lists.
- This is a problem because Quicksort will be called on lots of small lists.

• Fix 1: Use Insertion Sort on small problems.

• Fix 2: Leave small problems unsorted. Fix with one final Insertion Sort at end.

Why? Insertion Sort is very fast on almost-sorted lists

What’s the average case of quick sort if we get poorly balanced partitions like

T(n) <= T(9n/10) + T(n/10) + Q(n)?

Give the intuition for this.

Still get Q(n lg n)!! (As long as the split is of constant proportionality.)

Intuition: Can divide n by c > 1 only Q(lg n) times before getting 1

Quick sort Partition produces a mix of “good” and “bad” splits, distributed randomly in the recursion tree.

• What is the running time likely to be in such a case?

When splits alternate between good and bad, the cost of bad split can be absorbed into the cost of good split.

Thus the running time is O(n lg n), though with larger hidden constants.

Bad then Good split:

Q(n) + Q(n-1) = Q(n)

Good split from start:

Q(n)

Name 2 methods two randomize quicksort (Want to make running time independent of input ordering)

- Make the algorithm randomized.

2. Make every possible input equally likely.

Explain how we can make every possible input equally likely in quicksort.

For quicksort, it is sufficient if we can ensure that every

element is equally likely to be the pivot.

Give 2 fixes to the quicksort problem: Input distribution may not be uniformly random.

• Fix 1: Use “randomly” selected pivot.

• Fix 2: Median-of-three Quicksort

– Use median of three fixed elements (say, the first,

middle, and last) as the pivot.

– To get O(n^2) behavior, we must continually be unlucky to see that two out of the three elements examined are among the largest or smallest of their sets.

let X = # comparisons over all calls to Partition. Analyze the average case of Randomized quicksort.

Slide 24-25 lecture 22

O(nlgn)

Prove this claim:

zi and zj are compared iff the first element to be chosen as a pivot from Zij is either zi or zj.

The only comparison made are made with the pivot in the partition code.

Use contradiction: if it isn’t the pivot, you can’t compare them in the code.

Does probabilistic analysis give average running time or worst-case running time?

average running time.