DSAVisualiseData Structures & Algorithms Studio
Chapter 03 · sorting, selection, binary-search

Sorting & Selection

Sorting organizes collections into total order; selection locates the k-th smallest element without fully sorting. Merge Sort achieves deterministic O(N log N) worst-case comparison time with stability; Quicksort partitions in-place with O(N log N) expected time; Radix Sort sorts keys digit-by-digit in O(W·N) non-comparison time.

Key Invariants & Rules

  • Top-Down Merge Sort divides recursively at midpoint floor((lo + hi)/2) and merges sorted halves stably.
  • 3-way Quicksort partitions elements into strictly smaller, equal, and strictly greater bands.
  • Radix Sort preserves duplicate stability across bitwise passes from LSB to MSB.
  • Quickselect partitions only the sub-array containing the target rank k, achieving O(N) average time.
  • Tied comparisons never invert original relative order in stable algorithms.

Must-Know Algorithms

Merge SortO(N log N) time, O(N) space

Guaranteed optimal comparison bound, highly predictable, and fundamentally stable.

3-Way QuicksortO(N log N) avg, O(N) on duplicates

Dutch National Flag partition handles heavy duplicate keys gracefully in-place.

Binary Radix SortO(W·N) time, O(N) space

Non-comparison sort grouping integers by bit patterns across 32 binary passes.

Learning Path (10 Lessons)

Self-Check & Concept Review

1. Why does Top-Down Merge Sort require an auxiliary array of size N?

Merging two adjacent sorted halves in place without extra space is inefficient; copying to an auxiliary array enables a clean single-pass O(N) merge.

2. What is the advantage of 3-way partitioning (lt, i, gt) over standard 2-way partitioning in Quicksort?

Keys equal to the pivot are clustered together in one pass and never revisited in subsequent recursive calls.

3. How does Quickselect achieve O(N) average time compared to sorting the whole array?

Quickselect discards the partition that does not contain the target rank, resulting in a geometric series N + N/2 + N/4 + ... = 2N = O(N).

4. Why must Radix Sort use a stable inner sorting algorithm on each digit/bit pass?

If a pass rearranged ties arbitrarily, the sorted order from earlier less-significant digits would be lost.

5. In linked list merge sort, why is extra auxiliary array space not required?

Unlike contiguous array elements, linked list nodes can be spliced and merged simply by adjusting .next pointers without allocating new node wrappers.