Data Structures and Algorithms (H) Lecture 6 Notes
Randomization and Lower Bounds
A Randomized Version of QuickSort
Choosing the right pivot element can be tricky since we have no idea
a priori which element can be a good pivot. A (surprisingly good)
solution is to leave it to chance: 
Performance of
Randomized-QuickSort
First we assume that in the following that all elements in the array
are distinct. Now we may ask: what is a worst-case input for
Randomized-QuickSort? The answer is that there is no
worst case for Randomized-QuickSort! The reason behind
this is simple: all inputs lead to the same runtime behavior. -
The
Runtime of Randomized Algorithms
For randomized algorithms (in contrast to deterministic
algorithms) we consider the expected running time
Tip
For more information on expectations, random variables, and probabilities, see the notes for STA219 Probability and Statistics for Engineering.
For analyzing sorting algorithms the number of comparisons of elements made is an interesting quantity:
- For
QuickSortand other algorithms it can be used as a proxy or substitute for the overall running time.- Analyzing the number of comparisons might be easier than analyzing the number of elementary operations.
- Comparisons can be costly if the keys to be compared are not numbers, but more complex object.
- Algorithms making fewer comparisons might be preferable, even if the overall runtime is the same.
- There is a lower bound for the running time of all sorting algorithms that rely on comparisons only.
Let QuickSort. Since
comparisons are elementary operations,
For each comparison QuickSort only makes for loop. So other operations sum to
Expected Time for
Randomized-QuickSort
Theorem
The expected number of comparisons of
Randomized-QuickSort is
Proof. for ease of analysis, rename array elements to
A key observation is that each pair of elements is compared
at most once. The reason is that elements are only compared
against the pivot, and after Partition ends the pivot is
never touched again.
Let
- If pivot is
or then the decision whether to compare is postponed to a recursive call. - If pivot is
or , then they are compared. - If pivot is
then and become separated and are never compared.
A decision is only made if
Random Input vs. Randomized Algorithm
QuickSort is efficient if:
- The input is random or
- the pivot element is chosen randomly.
Key Insight
We have no control over the former, but we can make the latter happen.
- Deterministic
QuickSort- Pro: the runtime is deeterministic for each input
- Con: may be inefficient on some inputs
- Randomized
QuickSort- Pro: same behavior on all inputs
- Con: runtime is random, running it twice gives different times
Other Applications of Randomization
- Random Sampling
- Great for big data
- Sample likely reflects properties of the set it is taken from
- Symmetry breaking
- Vital for many distributed algorithms
- Randomized search heuristics
- General-purpose optimizers, great for complex problems
Comparison Sorts
So far we have learnt the following sorting algorithms:
InsertionSortSelectionSortMergeSortHeapSortQuickSort
All these algorithms proceed by comparing elements, hence the name
comparison sorts. The best runtime we have achieved on such
algorithms is
A Very Brief Tast of Complexity Theory
Complexity theory deals with the difficulty of problems. Or
more formally, it studies the limits to the efficiency of
algorithms. The complexity theory often gives results like: every
algorithm needs at least time
A famous concept in the complexity theory is the NP-Completeness and NP-Complete Problem. This concept first arises from the study of Entscheidungsproblem (German for decision problem). An example of the decision problem is
Example
Does there exist an assignment of variables that satisfies a Boolean formula?
The NP-complete problems:
- contain >3000 important problems in different shapes.
- is easy to verify that a given solution means “yes”.
- no one knows how to find a solution in polynomial worst-case time.
- either no NP-complete problem is solvable in polynomial time, or all of them are.
To show that a given time
Comparison Sorts as Decision Trees
From the previous sections we know that we can take the number of
comparisons as lower time bound. w.l.o.g. we assume that elements
A decision tree reflects all comparisons a particular comparison sort makes, and how the outcome of one comparison determines future comparisons.
In the decision tree, a inner node
An example of a decision tree: 
Theorem
Every comparison sort requires
Proof. The worst-case number of comparisons equals the
length of the longest simple path fomr the root to any
reachable leaf, i.e. the height
Every correct algorithm must be able to produce a sorted output for
each of the
So the worst-case number of comparisons is at least
- Title: Data Structures and Algorithms (H) Lecture 6 Notes
- Author: Zhige Chen
- Created at : 2025-10-15 21:45:59
- Updated at : 2025-10-15 21:50:55
- Link: https://nofe1248.github.io/2025/10/15/dsaa-lec-6/
- License: This work is licensed under CC BY-NC-SA 4.0.