∑GateAndNet
Practice MCQs
GATE CS / Algorithms
Edit
INTERMEDIATE🔥 High-Yield Exam Topic 4 min read 3 views

Sorting Bounds, Stability, Inversions & P / NP / NP-Complete Reductions

Decision-tree Omega(n log n) lower bound, stability & in-place comparison table, counting array inversions, and polynomial-time Karp reductions (P, NP, NP-Hard, NP-Complete).

1. Complete Sorting Algorithms Cheat Matrix

AlgorithmBestAverageWorstAux SpaceStable?
Insertion SortΘ(n)\Theta(n)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)O(1)\mathcal{O}(1)Yes
Merge SortΘ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)Θ(n)\Theta(n)Yes
Heap SortΘ(nlog⁡n)\Theta(n \log n) (or Θ(n)\Theta(n) all equal)Θ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)O(1)\mathcal{O}(1)No
Quick SortΘ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)Θ(n2)\Theta(n^2)O(log⁡n)\mathcal{O}(\log n) stackNo
Counting SortΘ(n+k)\Theta(n + k)Θ(n+k)\Theta(n + k)Θ(n+k)\Theta(n + k)Θ(n+k)\Theta(n + k)Yes
  • Inversions: A pair (i<j)(i < j) with A[i]>A[j]A[i] > A[j]. Maximum inversions in nn elements =(n2)= \binom{n}{2} (reverse sorted). Insertion Sort takes Θ(n+I)\Theta(n + I) time where II is the number of inversions!

2. Complexity Classes: P, NP, NP-Complete & NP-Hard

  • PP: Decision problems solvable by a Deterministic Turing Machine in polynomial time O(nk)\mathcal{O}(n^k). (Shortest Path, MST, 2-SAT, Eulerian Circuit, Linear Programming).
  • NPNP: Decision problems whose "YES" certificates can be verified in deterministic polynomial time (equivalently solved by a Non-Deterministic TM in polynomial time). Note: P⊆NPP \subseteq NP.
  • NPNP-Hard: At least as hard as every problem in NPNP (∀X∈NP,  X≤pH\forall X \in NP,\; X \le_p H). Need not even be in NPNP (e.g., the Halting Problem is Undecidable AND NPNP-Hard!).
  • NPNP-Complete (NPCNPC): L∈NP  ∧  L∈NP-HardL \in NP \;\land\; L \in NP\text{-Hard}. Classic NPCNPC problems: 3-SAT, Clique, Vertex Cover, Independent Set, Hamiltonian Cycle, Subset Sum, 0/1 Knapsack (Decision), Graph kk-Coloring (k≥3k \ge 3).
📐 Quick Revision Formula Sheet
Comparison Sorting Decision-Tree Lower Bound
2h≥n!  ⟹  h≥log⁡2(n!)=Ω(nlog⁡n)2^h \ge n! \implies h \ge \log_2(n!) = \Omega(n \log n)
💡 Applies to EVERY comparison-based sort in the worst & average case
NP-Hardness Polynomial Reduction Direction
A≤pB  ∧  A∈NP-Hard  ⟹  B∈NP-HardA \le_p B \;\land\; A \in \text{NP-Hard} \implies B \in \text{NP-Hard}
💡 Always reduce a KNOWN hard problem A INTO target problem B!
#Sorting#QuickSort#MergeSort#Inversions#NP-Completeness#Reductions

🎯 Practice PYQs on This Topic (2)

Q1.GATE CS AlgorithmsMCQEASY
+1 Marks • -0.33 Neg

Which sorting algorithm performs the minimum number of comparisons (Θ(n)\Theta(n)) when the input array is already sorted in ascending order?

Q2.UGC NET / GATE AlgoMCQMEDIUM
+2 Marks • -0.67 Neg

Suppose problem AA is known to be NP-Complete, and we want to prove that a new decision problem B∈NPB \in \text{NP} is also NP-Complete. Which polynomial-time reduction must we construct?

Previous TopicDynamic Programming Masterclass: 0/1 Knapsack, LCS & Matrix Chain MultiplicationNext Topic Formal Languages Closure Properties & Decidability Master Matrix