ALGORITHMS/Student Engineering Notebook
Page 04·notes
SORTING, SELECTION & PRIORITY QUEUES—~34 MIN READ

Sorting Bounds, Non-Comparison Sorting & Binary Heaps

“Omega(n log n) decision tree bound, Counting/Radix/Bucket sort, linear-time Build-Heap, and Heap Sort.”

1.The Decision Tree Lower Bound for Comparison Sorting

In a comparison sort, we only gain information about the input sequence (a_1, a_2, ..., a_n) by pairwise comparisons a_i <= a_j.

A decision tree models the execution of any comparison sort on n inputs:

- Each internal node represents a comparison a_i : a_j.

- Each leaf represents a unique permutation of the input elements.

Since any of the n! permutations could be the correct sorted order, the tree must contain at least n! reachable leaves (L >= n!).

A binary tree of height h has at most 2^h leaves. Therefore:

2^h >= L >= n! implies h >= log_2(n!)

Using Stirling's approximation (n! >= (n/e)^n):

h >= log_2((n / e)^n) = n log_2 n - n log_2 e = Omega(n log n).

Hence, any comparison sort requires at least Omega(n log n) comparisons in the worst case.

2.Mathematical Proof of Linear-Time Build-Heap

A common misconception is that building a heap of n elements takes O(n log n) because each Max-Heapify call takes O(log n) on n/2 nodes.

The key insight is that most nodes in a heap are close to the leaves and have very small heights h:

- An n-element heap has height lfloor log_2 n rfloor.

- At height h, there are at most lceil n / 2^{h+1} rceil nodes.

- Calling Max-Heapify on a node of height h takes O(h) time.

Total time to build the heap:

T(n) <= sum_{h=0}^{lfloor log_2 n rfloor} lceil frac{n}{2^{h+1}} rceil O(h) <= c n * sum_{h=0}^infty frac{h}{2^h}

Evaluating the infinite series S = sum_{h=0}^infty frac{h}{2^h}:

S = 1/2 + 2/4 + 3/8 + 4/16 + ...

S/2 = 1/4 + 2/8 + 3/16 + ...

S - S/2 = S/2 = 1/2 + 1/4 + 1/8 + ... = 1 implies S = 2.

Therefore: T(n) <= c n (2) = O(n). Building a binary heap takes strictly linear time!

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

3. Worked Problem: Maximum Swaps in Build-Heap

⚠️ Pitfall Warning:

Heapify operates top-down from index floor(n/2) down to 1. Count height from leaves (leaves are height 0).

Problem: For an array of size n = 15 arranged in strictly ascending order, calculate the maximum number of element swaps performed by Build-Max-Heap.

Step 1: Characterize tree structure for n = 15:

Complete binary tree of 4 levels (perfect binary tree):

- Height 0 (leaves): 8 nodes (indices 8 to 15). Heapify does 0 swaps.

- Height 1: 4 nodes (indices 4, 5, 6, 7). Each can swap at most 1 time.

- Height 2: 2 nodes (indices 2, 3). Each can swap at most 2 times.

- Height 3 (root): 1 node (index 1). Can swap at most 3 times.

Step 2: Ascending array worst-case scenario:

Because array is ascending, parent elements are smaller than all descendants.

Every non-leaf node will trickle down to the leaf level.

Step 3: Calculate total swaps:

Total swaps = (4 1) + (2 2) + (1 * 3) = 4 + 4 + 3 = 11 swaps.

Final Answer: Exactly 11 swaps are performed.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: The general bound log₂(n!) = Θ(n log n) is why comparison sorting can't beat n log n.

Worked example: the comparison lower bound for 5 keys

Problem Statement:
What is the minimum number of comparisons any comparison sort needs, in the worst case, to sort 5 distinct keys?
Step 1: Model the sort as a decision tree

Every comparison sort is a binary tree of comparisons, and each of the n! orderings must reach a different leaf.

Step 2: Bound the height

A binary tree of height h has at most 2^h leaves, so 2^h ≥ n!.

h ≥ log₂(n!)
Step 3: Plug in n = 5

5! = 120 and log₂ 120 ≈ 6.91.

h ≥ ⌈6.91⌉
↳ Result: h ≥ 7
Final Answer:At least 7 comparisons
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · FOUNDATION

What is the minimum worst-case number of comparisons needed to sort 4 distinct keys?

Problem 2 · GATE-LEVEL

Counting sort sorts n integers in the range 0 to k. What is its running time?

Problem 3 · TRICKY

Building a binary heap from n unsorted keys bottom-up takes how long?

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Introduction to Algorithms (4th Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Abdul Bari · Algorithms↗Instructor: Abdul Bari · Covers: Sorting, graph algorithms, dynamic programming, greedy, backtracking

Best visual explanations of algorithm design and complexity analysis.

Open YouTube↗
Gate Smashers · Algorithms for GATE↗Instructor: Varun Singla · Covers: Asymptotic analysis, Master Theorem, DP, Dijkstra, Bellman-Ford

Exam-focused with GATE PYQ numericals on every major algorithm.

Open YouTube↗