Professional Documents
Culture Documents
Study selection sort algorithm to understand it, determine which steps are worth counting, and do
some runtime analysis.
Essence of selection sort for list of n values:
locate largest of the n values and swap it with whatever is in position n.
locate largest of the first n-1 values and swap it with whatever is in position n-1.
locate largest of the first n-2 values and swap it with whatever is in position n-2.
locate largest of the first n-3 values and swap it with whatever is in position n-3.
repeat until you’ve found larger of first 2 and swapped.
At the end of any pas through the list, list is partitioned into two parts: the unsorted part followed
by the sorted part.
The sorted part starts at the end and grows toward the beginning. You are finished when it
reaches the beginning.
Note this uses the “search for the largest” algorithm from lab 3.
What operations appear to be most significant?
Comparisons (to find the largest) and exchanges.
Runtime analysis of selection sort.
How many comparisons are done?
Any difference between worst and best case for comparisons? Nope
To find largest of n values requires n-1 comparisons, but that covers only the first repetition.
The total comparisons is (n-1)+(n-2)+(n-3)+…+3+2+1, which is n * (n-1) / 2.
How many exchanges are done?
Any difference between worst and best case? No.
One exchanges is done per "pass" through the list, for total of (n-1).
Total comparisons and exchanges: n * (n-1) / 2 + (n – 1) = O(n2)
Bubble sort
Essence of bubble sort for list of n values V1 through Vn:
Make a beginning-to-end pass through the list from V1 through Vn. For each pair of values
Vi and Vi+1, compare them and exchange if out of order. Largest value ends up in
position n.
Make second pass through the list from V1 through Vn-1. For each pair of values Vi and
Vi+1, compare them and exchange if out of order. Second-largest value ends up in
position n-1.
Make third pass through the list from V1 through Vn-2. For each pair of values Vi and Vi+1,
compare them and exchange if out of order. Third-largest value ends up in position n-2.
Continue until n-1 passes have been made.
See where it gets its name? In the first pass, the largest value “bubbles” its way to its correct
position.
At the end of any given pass, the list is partitioned into the unsorted part followed by the sorted
part.
Runtime analysis of bubble sort.
How many comparisons are done?
Any difference between worst and best case for comparisons? No
First pass requires n-1 comparisons, second pass n-2, third pass n-3, for total of n-1 passes.
The total comparisons is (n-1)+(n-2)+(n-3)+…+3+2+1, which is n * (n-1) / 2.
How many exchanges are done?
Any difference between worst and best case? Yes.
Best case exchanges: values originally ordered, no exchanges.
Worst case exchanges: values in reverse order, exchange for every comparison.
Best case total comparisons and exchanges: n * (n-1) / 2 + 0 = O(n2)
Worst case total comparisons and exchanges: 2 * (n * (n-1) / 2 ) = O(n2)
NOTE: Best case can be reduced to n-1 comparisons and 0 exchanges using this simple
optimization: set “exchanged” flag to false at beginning of pass and set it true if exchange is
made. If still false at end of pass then list is ordered so stop. In the best case, list is originally
ordered, so it stops after first pass.
Insertion Sort
Informal description:
Make n-1 passes through list
At the start of pass i, the list is partitioned into a sorted part, the first i values and the unsorted
part, the remaining n-i values.
In pass i, “pick up” the first value of the unsorted part (position i+1) and work backwards
into the sorted part, exchanging it with each value along the way until correct position is
found.