CS 2100 · DSA1 · Lab 08

Sorting·Solved

A Worked Companion / 2026
Merge  ·  Quick  ·  Eight identifications  ·  Every step shown
01
MergeSort, top to bottom.
Every split, every merge.
O(n log n)

We recursively halve the vector until each piece is a single element, then merge the pieces back together in sorted order. Splits descend; merges ascend.

Input
952276518335824616602
chunk merged up final sorted
Split 1 — halve (12 → 6 + 6)
95 22 76 51 8 33
5 82 46 16 60 2
Split 2 — halve again (6 → 3 + 3)
95 22 76
51 8 33
5 82 46
16 60 2
Split 3 — halve again (3 → 2 + 1)
95 22
76
51 8
33
5 82
46
16 60
2
Split 4 — base case, singletons
95
22
76
51
8
33
5
82
46
16
60
2
Merge 1 — pairs into twos
22 95
76
8 51
33
5 82
46
16 60
2
Merge 2 — twos+singletons into threes
22 76 95
8 33 51
5 46 82
2 16 60
Merge 3 — threes into sixes
8 22 33 51 76 95
2 5 16 46 60 82
Merge 4 — final merge, sorted
2   5   8   16   22   33   46   51   60   76   82   95
Merge trace — how the final merge works
Merging [8, 22, 33, 51, 76, 95] with [2, 5, 16, 46, 60, 82] — compare heads, take smaller:
take 2 8 vs 2 → 2
take 5 8 vs 5 → 5
take 8 8 vs 16 → 8
take 16 22 vs 16 → 16
take 22 22 vs 46 → 22
take 33 33 vs 46 → 33
take 46 51 vs 46 → 46
take 51 51 vs 60 → 51
take 60 76 vs 60 → 60
take 76 76 vs 82 → 76
take 82 95 vs 82 → 82
take 95 only 95 remains → 95
02
QuickSort, first element as pivot.
Every recursive call, every swap.
avg O(n log n)

Partition around the leftmost element. Elements < pivot go left, elements > pivot go right, then recurse on each side. The pivot lands in its final resting place each call.

Input
952276518335824616602
pivot < pivot > pivot placed (final)
Call 1 · quicksort( [0 … 11] ) · pivot = 95
95 is the largest value — every other element is < 95. After partitioning, 95 swaps with the rightmost element and lands at index 11.
227651833582461660295
Swap: 95 ↔ 2. Recurse on left [22, 76, 51, 8, 33, 5, 82, 46, 16, 60, 2]. Right side is empty.
Call 2 · quicksort( [0 … 10] ) · pivot = 22
Scan: values < 22 are {8, 5, 16, 2}; values > 22 are {76, 51, 33, 82, 46, 60}. Pivot 22 settles at index 4.
8516222765133824660
Recurse left on [8, 5, 16, 2], right on [76, 51, 33, 82, 46, 60].
Call 3 · quicksort( [0 … 3] ) · pivot = 8
52816
Left: [5, 2]. Right: [16] (base case).
Call 4 · quicksort( [0 … 1] ) · pivot = 5
25
[2] is a base case. Left partition of Call 2 is now 2 5 8 16.
Call 5 · quicksort( [6 … 10] ) · pivot = 76
513346607682
Left: [51, 33, 46, 60]. Right: [82] (base case).
Call 6 · quicksort( [6 … 9] ) · pivot = 51
33465160
Left: [33, 46]. Right: [60] (base case).
Call 7 · quicksort( [6 … 7] ) · pivot = 33
3346
No values < 33. Right: [46] (base case). Right partition of Call 2 resolves to 33 46 51 60 76 82.
Final, sorted
258162233465160768295
03
QuickSort’s worst case.
When the elegant algorithm degrades.
O(n²)

QuickSort’s efficiency comes from balanced partitions. When the pivot is always the smallest or largest element, the recursion tree becomes a degenerate chain instead of a branching tree.

Worst case: already-sorted (or reverse-sorted) input with a first/last element pivot.

With the first element as pivot on a sorted vector like [2, 5, 8, 16, 22, …], the pivot is always the minimum. Every partition produces an empty left side and a right side of size n − 1.

That makes n recursive calls, each doing O(n) work — total O(n²). Reverse-sorted input behaves the same way because the pivot is always the maximum.

Side note — how to avoid it

In practice, the worst case is sidestepped by picking the pivot more carefully: randomly, as the median-of-three (first, middle, last), or by shuffling before sorting. These tricks don’t change the theoretical worst case, but they make it vanishingly unlikely on real inputs.

04
Identify the sort.
Eight fingerprints, eight algorithms.
A–H multiple choice
Initial contents of S (18 values)
50219435128851014759821028619173669
01
After 4 passes, S becomes:
12215354750591028619173669828894101
Why: Bubble sort floats the largest remaining value to the back on each pass. After 4 passes, the 4 largest values 82, 88, 94, 101 are locked in at the tail in that exact order — the telltale fingerprint. The front 14 elements are still churning: partially shuffled by adjacent swaps but not yet sorted.
Answer
Bubble sort
C
02
After 2 levels of recursive calls, S becomes:
12213550945478810110285961829173669
Why: MergeSort splits 18 → 9+9, then 9 → 5+4 (or 4+5). After two levels of recursion return, we have four sorted sub-runs: [12 21 35 50 94], [5 47 88 101], [10 28 59 61 82], [9 17 36 69]. Each chunk is internally sorted — textbook merge signature.
Answer
MergeSort
D
03
After 3 passes, S becomes:
91051217213635285061475969828894101
Why: This isn’t purely bubble (far too many elements already in place) and isn’t insertion (the front isn’t sorted — 5 is out of order). It’s Shell sort: after sorting with decreasing gaps, elements are mostly in order with local inversions like 10 5 and 36 35. The tell is large-scale order with small local disorder.
Answer
Shell sort
H
04
After 4 passes, S becomes:
12213550948851014759821028619173669
Why: Insertion sort grows a sorted prefix by one each pass. Starting from 50, after 4 passes (inserting 21, 94, 35, 12) the first five elements are 12 21 35 50 94 — sorted — and the rest of the tail is untouched. Perfect insertion signature.
Answer
Insertion
A
05
After 1 pass, S becomes:
50102110161128294355364717882859969
Why: Read the last digits: 50, 10, 21, 101, 61, 12, 82, 94, 35, 5, 36, 47, 17, 88, 28, 59, 9, 69 — that’s 0,0,1,1,1,2,2,4,5,5,6,7,7,8,8,9,9,9, perfectly ordered. The array is sorted by ones digit.
Answer
Radix
G
06
After 2 levels of recursive calls, S becomes:
17213512510289364750626188598294101
Why: QuickSort signature — pivots land in final position while surrounding regions are unsorted but partitioned. 50 is in its final sorted index with everything smaller to its left and everything larger to its right. Merge would leave its chunks internally sorted; this doesn’t.
Answer
QuickSort
E
07
After 5 passes, S becomes:
59101217885010147598294286121353669
Why: Selection sort finds the minimum of the unsorted tail each pass and places it at the front. After 5 passes, the five smallest values 5, 9, 10, 12, 17 sit at the front in order, and the tail is in its original relative order minus those five — which matches exactly.
Answer
Selection
B
08
After 4 passes, S becomes:
69596147502835361791210215828894101
Why: Heap sort repeatedly extracts the max to the end. After 4 passes the four largest values 82, 88, 94, 101 are sorted at the rear. The messy-looking front is the remaining heap — notice it’s not sorted like bubble would produce, but the parent-greater-than-child heap property holds (69 ≥ 59, 61; 59 ≥ 47, 50; etc.).
Answer
Heap sort
F
✦   ✦   ✦