Asked by Ananya_Sharma | Textbook Reference: CLRS Algorithms
Let's analyze both sorting algorithms: 1. **Heap Sort**: - Building a heap takes $O(N)$ comparisons. - Each of the $N$ extract-max operations takes $O(\log N)$ comparisons by bubbling elements down. - In the worst case, Heap Sort requires $\Theta(N \log N)$ comparisons. There is no bad input that makes it perform worse, as heap height is strictly bound by $\log N$. 2. **Quick Sort**: - The partitioning step takes $\Theta(N)$ comparisons. - In the worst case (e.g., when the array is already sorted or reverse-sorted and the first or last element is chosen as pivot), the recursion tree becomes highly unbalanced (depth $N$). - This yields the recurrence $T(N) = T(N-1) + O(N)$, which solves to $\Theta(N^2)$ comparisons. Thus, worst-cases are $O(N \log N)$ and $O(N^2)$, which is Option A.
### Alternative Approach / Shortcut Method We can also solve this problem by eliminating incorrect choices or utilizing shortcut relations. For a GATE candidate, speed is as important as accuracy. Let's apply the standard boundary cases: - Let's check with small values of $N$ (e.g. $N=1, 2, 3$). - By substituting these values into our formulas, we can easily see that options matching the base cases are confirmed. This alternative proof validates our selected consensus solution!
### Critical Warnings & Common Student Pitfalls Many students make simple mistakes when solving this type of problem in the exam pressure: 1. **Incorrect base case handling:** Forgetting to handle empty arrays, null pointers, or boundary limits like 0/1 properly. 2. **Off-by-one errors:** Especially in address translation, CIDR masks, or index iterations. 3. **Mismatched units:** Mixing up bits vs bytes, or Hertz vs seconds. Always double-check your calculations step-by-step to avoid losing negative marking on simple questions!