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
§ 2.1.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
§ 2.2.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!
3. Worked Problem: Maximum Swaps in Build-Heap
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.
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 for clarity, rigorous numerical solving, and semester excellence:
Best visual explanations of algorithm design and complexity analysis.
Exam-focused with GATE PYQ numericals on every major algorithm.