# Sorting Bounds, Stability, Inversions & P / NP / NP-Complete Reductions
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Algorithms
Canonical URL: https://gateandnet.com/exams/gate-cs/algorithms/sorting-bounds-and-np-completeness

> 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

| Algorithm | Best | Average | Worst | Aux Space | Stable? |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **Insertion Sort** | $\Theta(n)$ | $\Theta(n^2)$ | $\Theta(n^2)$ | $\mathcal{O}(1)$ | **Yes** |
| **Merge Sort** | $\Theta(n \log n)$ | $\Theta(n \log n)$ | $\Theta(n \log n)$ | $\Theta(n)$ | **Yes** |
| **Heap Sort** | $\Theta(n \log n)$ (or $\Theta(n)$ all equal) | $\Theta(n \log n)$ | $\Theta(n \log n)$ | $\mathcal{O}(1)$ | **No** |
| **Quick Sort** | $\Theta(n \log n)$ | $\Theta(n \log n)$ | $\Theta(n^2)$ | $\mathcal{O}(\log n)$ stack | **No** |
| **Counting Sort** | $\Theta(n + k)$ | $\Theta(n + k)$ | $\Theta(n + k)$ | $\Theta(n + k)$ | **Yes** |

- **Inversions:** A pair $(i < j)$ with $A[i] > A[j]$. Maximum inversions in $n$ elements $= \binom{n}{2}$ (reverse sorted). Insertion Sort takes $\Theta(n + I)$ time where $I$ is the number of inversions!

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

- **$P$:** Decision problems solvable by a **Deterministic** Turing Machine in polynomial time $\mathcal{O}(n^k)$. (Shortest Path, MST, 2-SAT, Eulerian Circuit, Linear Programming).
- **$NP$:** Decision problems whose "YES" certificates can be **verified** in deterministic polynomial time (equivalently solved by a **Non-Deterministic** TM in polynomial time). Note: $P \subseteq NP$.
- **$NP$-Hard:** At least as hard as every problem in $NP$ ($\forall X \in NP,\; X \le_p H$). Need **not** even be in $NP$ (e.g., the **Halting Problem** is Undecidable AND $NP$-Hard!).
- **$NP$-Complete ($NPC$):** $L \in NP \;\land\; L \in NP\text{-Hard}$. Classic $NPC$ problems: **3-SAT, Clique, Vertex Cover, Independent Set, Hamiltonian Cycle, Subset Sum, 0/1 Knapsack (Decision), Graph $k$-Coloring ($k \ge 3$)**.

## Solved Practice Questions & PYQs

### [GATE CS Algorithms | MCQ]
Which sorting algorithm performs the **minimum number of comparisons ($\Theta(n)$)** when the input array is **already sorted** in ascending order?
**Correct Answer:** C
**Solution:** On an already sorted array of size $n$, **Insertion Sort** compares each element $A[i]$ only once with $A[i-1]$ ($n - 1$ comparisons total $= \Theta(n)$), whereas Merge/Heap take $\Theta(n \log n)$ and last-pivot Quick Sort degrades to $\Theta(n^2)$!

### [UGC NET / GATE Algo | MCQ]
Suppose problem $A$ is known to be **NP-Complete**, and we want to prove that a new decision problem $B \in \text{NP}$ is also **NP-Complete**. Which polynomial-time reduction must we construct?
**Correct Answer:** B
**Solution:** To prove $B$ is at least as hard as $A$, you must reduce the **known NP-Complete problem $A$ TO $B$ ($A \le_p B$)** so any polynomial solver for $B$ would immediately solve $A$ in polynomial time.
