Algorithms · Sorting Beyond the Lecture: Linear-Time Sorts, Lower Bound, Selection and the Full Comparison
Bubble Sort (with the swapped flag)
This chapter picks up where "Merge Sort and Quick Sort" stops. We finish the elementary sorts (bubble with the swapped flag, selection with exact counts, shell sort), then cross the wall that comparison sorting cannot cross — the lower bound — and go around it with counting, radix and bucket sort. Then we turn sorting inside out: instead of sorting everything, we select (min, max, second largest, -th smallest) with exact comparison counts. Finally, one master table for every sort, the GATE question patterns, and the traps.
Idea. Repeatedly walk the array left to right; whenever two neighbours are out of order, swap them. After one full pass the largest element has "bubbled" to the last position. After pass , the last elements are final.
The swapped flag. If a pass makes no swap, the array is already sorted — stop. This makes bubble sort on already-sorted input.
BubbleSort(A[0..n-1]):
for i = 0 to n-2:
swapped = false
for j = 0 to n-2-i: // last i elements already final
if A[j] > A[j+1]:
swap(A[j], A[j+1])
swapped = true
if swapped == false: break // early exit
Exact counts (worst case, reverse-sorted input).
- Comparisons: .
- Swaps: every comparison causes a swap, so also .
Best case (already sorted, with flag). Pass 1 does comparisons, 0 swaps, then exits: comparisons, 0 swaps. Without the flag it is still — this is a favourite GATE trap.
Stability. Only swaps adjacent elements when strictly out of order (), so equal elements never cross: stable. In place: yes, extra space.
Selection Sort — Exact Comparison and Swap Counts
Idea. For each position from to , find the minimum of and swap it into position .
SelectionSort(A[0..n-1]):
for i = 0 to n-2:
min = i
for j = i+1 to n-1:
if A[j] < A[min]: min = j
if min != i: swap(A[i], A[min])
Comparisons — always exactly the same, no best/worst difference:
Swaps — at most (one per outer iteration), and exactly in the worst case. This is the key contrast with bubble/insertion: selection sort does comparisons but only swaps, which matters when writes are expensive.
Worked example. , . Comparisons per pass: . ✓
| Pass | Array before | min found | Swap? | Array after |
|---|---|---|---|---|
| 0 | 64 25 12 22 11 | 11 (index 4) | yes | 11 25 12 22 64 |
| 1 | 11 25 12 22 64 | 12 (index 2) | yes | 11 12 25 22 64 |
| 2 | 11 12 25 22 64 | 22 (index 3) | yes | 11 12 22 25 64 |
| 3 | 11 12 22 25 64 | 25 (index 3) | no | 11 12 22 25 64 |
Swaps = 3 (≤ ). ✓
Stability. The long-distance swap can jump an equal element over its twin: → swap 2_a with 1 → — order of the 2's reversed. Not stable. In place: yes.
Shell Sort — the Idea
Insertion sort is fast on nearly sorted arrays (it moves an element only past elements it must pass). Shell sort exploits this: sort subsequences with a gap , shrinking to 1.
- Pick a gap sequence (e.g. ).
- For each gap , do an insertion sort on each of the interleaved subsequences
- The final gap-1 pass is ordinary insertion sort, but on an array that is now nearly sorted, so it is cheap.
Complexity. Depends entirely on the gap sequence; the exact worst case is a research topic, so GATE does not ask for a closed form. What GATE does ask: shell sort is in place ( extra space), not stable (elements with equal keys in different gap-subsequences can be reordered), and its last pass is insertion sort.