Sorting Algorithms Explained: Stability, Complexity and Worked Traces for GATE CS

Learn to classify sorting methods, trace insertion sort by hand, compare time and space costs, and avoid common lower-bound and stability traps.

KnowledgeGate Team

Exam prep & CS education

Updated 1 Dec 20265 min read
Sorting Algorithms Explained: Stability, Complexity and Worked Traces for GATE CS

Sorting questions become difficult when a small change in wording changes the correct answer. A question may combine an array trace, time complexity, stability, and auxiliary space instead of asking for one definition. Use Sorting Algorithms Compared: Complexity, Stability, and the n log n Lower Bound for a cross-family lookup, and Sorting Algorithms: Complete Guide with Worked Examples for GATE CS and Interviews for paired insertion and quicksort mechanics. The harder GATE CS reasoning is to keep record identities visible, calculate an exact decision-tree bound, and test whether the comparison model applies.

What a sorting problem actually specifies

A sorting input contains records and a key. In 6_A, 2_B, 6_C, 4_D, 1_E, 4_F, numbers are keys and letters identify records. Ascending order is 1_E, 2_B, 4_D, 4_F, 6_A, 6_C.

Exam questions often combine four axes:

  • Order: increasing or decreasing keys.

  • Stability: equal-key records retain their original relative order. A stable result keeps 4_D before 4_F and 6_A before 6_C.

  • Storage: an in-place implementation uses limited extra array storage; an out-of-place one creates auxiliary storage.

  • Method: comparison sorts learn order through pairwise comparisons; non-comparison sorts can use key values directly.

Properties depend on implementation. Counting sort needs stable output placement for stability. Quicksort may partition in place, but its recursion stack uses space.

Comparison sorting and the lower-bound idea

In a decision tree, each comparison has two outcomes. Sorting n distinct items must distinguish n! input orders, so the tree needs at least n! leaves. A worst-case path therefore has depth at least ceil(log2(n!)), giving the comparison-sorting lower bound Omega(n log n).

For n = 8, 8! = 40,320. Since 2^15 = 32,768 is too small but 2^16 = 65,536 is large enough, comparison-only sorting needs at least 16 comparisons in the worst case. Counting sort on integer keys 0..8 examines key values, not only pairs, so it does not violate the bound.

Decision-tree lower-bound figure for n = 8 compares 40,320 input orders with 2^15 = 32,768 and 2^16 = 65,536, concluding at least 16 comparisons.

Fully worked trace: insertion sort with two duplicate-key pairs

Five-pass insertion sort trace showing duplicate-key stability, 12 comparisons, and 9 shifts.

Use ascending insertion sort with the condition while A[j].key > key. The strict comparison stops at equality, which makes stability visible rather than merely asserted.

  1. Start: 6_A, 2_B, 6_C, 4_D, 1_E, 4_F.

  2. Pass 1, insert 2_B: compare with 6_A and shift once. Result: 2_B, 6_A, 6_C, 4_D, 1_E, 4_F.

  3. Pass 2, insert 6_C: compare with 6_A; equality stops the scan. Result unchanged: 2_B, 6_A, 6_C, 4_D, 1_E, 4_F.

  4. Pass 3, insert 4_D: compare with 6_C, 6_A, then 2_B; shift both 6 records. Result: 2_B, 4_D, 6_A, 6_C, 1_E, 4_F.

  5. Pass 4, insert 1_E: compare with 6_C, 6_A, 4_D, and 2_B; shift all four. Result: 1_E, 2_B, 4_D, 6_A, 6_C, 4_F.

  6. Pass 5, insert 4_F: compare with 6_C, 6_A, then 4_D; shift the two 6 records. Final: 1_E, 2_B, 4_D, 4_F, 6_A, 6_C.

The passes make 1 + 1 + 3 + 4 + 3 = 12 key comparisons and 1 + 0 + 2 + 4 + 2 = 9 shifts. Equality stops Pass 2 at 6_A and Pass 5 at 4_D, preserving both equal-key pairs. Replacing > with >= would reverse their relative order.

An already sorted six-item input needs five failed key comparisons and no shifts. Six reverse-sorted distinct items need 1 + 2 + 3 + 4 + 5 = 15 comparisons and 15 shifts. These boundary cases explain best-case Theta(n) and worst-case Theta(n^2).

Constraint matrix: reject the wrong sorting family first

Constraint

Candidate

Reason to reject it

Nearly sorted input and stability required

Insertion sort

Reject when a worst-case O(n log n) guarantee is mandatory

Worst-case O(n log n) and stability required

Merge sort on arrays

Reject when O(n) auxiliary array storage is unavailable

Worst-case O(n log n) with constant auxiliary array storage

Heap sort

Reject when stability is required

Small, dense integer key range

Counting sort

Reject when the key range is large or keys are only comparable

Quicksort remains a strong average-case array choice, but its pivot rule and recursion depth matter. Bubble and selection sort are useful trace models, not default large-input choices. Constraint-first selection avoids confusing a catalogue entry with a recommendation.

Choose by constraints: three exact scenarios

Nearly sorted data: On 1, 2, 3, 5, 4, 6, 7, 8, insertion sort shifts only 5 once when inserting 4. Adaptiveness matters even with a Theta(n^2) worst case.

Small integer range: Use Radix Sort and Counting Sort: Worked Examples, Complexity and Exam Traps for the full frequency, prefix, stable-placement, and radix-pass treatment. In a family-selection question, 5, 1, 4, 1, 0, 3 with keys 0..5 needs only the raw frequency array [1, 2, 0, 1, 1, 1] and the result 0, 1, 1, 3, 4, 5. Six records and six possible keys give Theta(n + k) = Theta(6 + 6). This uses key values directly, so the comparison lower bound does not apply.

Predictable comparison-sort time: For eight arbitrary records, standard merge sort has three merge levels, each processing eight records, when an extra array and stability are acceptable. If constant auxiliary array storage is required and stability is not, heap sort fits. There is no universal winner.

Traps that change a correct answer

Stability trap: An unstable sort may turn 3_A, 1_B, 3_C, 2_D into 1_B, 2_D, 3_C, 3_A. The keys are ordered, but equal-key records crossed. Track identities, not only values.

Case-analysis trap: First-element-pivot quicksort on sorted 1, 2, 3, 4, 5, 6, 7, 8 creates sizes 0 and 7, then 0 and 6, down to 0 and 1. Comparisons total 7 + 6 + 5 + 4 + 3 + 2 + 1 = 28, exposing this scheme's Theta(n^2) case.

Space and bound trap: Omega(n log n) constrains worst-case comparison sorting, not counting sort on bounded integer keys. An in-place partition does not make recursive quicksort's auxiliary space O(1).

How GATE-style practice tests sorting reasoning

IIT Guwahati's official GATE 2026 past test papers and syllabus page is the authority for that cycle. Use the official page, rather than a topic article, for paper structure, marks, counts, and later changes.

Test the reasoning with four short checks:

  1. After two insertion-sort passes on the running input? 2_B, 6_A, 6_C, 4_D, 1_E, 4_F. The first three records form the sorted prefix.

  2. Minimum worst-case comparisons for eight distinct items? 16, because 2^15 < 8! <= 2^16.

  3. What does the final order 4_D, 4_F and 6_A, 6_C demonstrate? Stability, because each equal-key relative order is preserved.

  4. First-pivot quicksort comparisons on the sorted eight-item input? 28, from 7 + 6 + 5 + 4 + 3 + 2 + 1.

KnowledgeGate currently offers more than 300 Sorting Algorithms questions, including more than 10 focused on Introduction to Sorting. The GATE CS Exam Preparation page connects that practice with wider paper coverage.

Short version and the next deliberate step

  • Identify whether the question uses comparisons or key values.

  • Trace records with identities so stability remains visible.

  • Compare best, average, and worst time together with auxiliary space.

  • Check whether the comparison lower bound actually applies.

Redo the six-record insertion trace without looking, then explain the 16-comparison bound aloud. For structured subject coverage, continue with GATE Guidance by Sanchit Sir. Solve the trace yourself before reading another summary.