Lecture 05: Practice Problems

1. Stability

Is standard Quicksort stable?

Show Answer

No.

Swapping elements across long distances (like partition does) changes relative order of equal keys.

2. Which Sort?

You need to sort an array that is "almost sorted" (only a few elements out of place). Which algorithm is likely fastest?

Show Answer

Insertion Sort.

It runs in O(n) for nearly sorted data.

Quicksort (with first element pivot) would actually be O(n^2)! Merge sort is always O(n log n).

3. Median of Medians

There exists a pivot selection strategy that guarantees O(n) worst-case time for Selection. Is it used in practice?

Show Answer

Mostly No.

The constant factor is very high. Randomized QuickSelect is usually faster.