Sorting and Searching

Comparison sorts are bounded below by O(n log n), achieved by merge sort and heap sort and by quicksort on average; binary search then finds an element in a sorted array in O(log n) by halving the range each step.

Key formulas & points

Skim these first — then read the full notes below.

  • A stable sort preserves the order of equal keys
  • Heap sort is O(n log n) in place but not stable
  • Counting and radix sort reach O(n) when the key range is bounded

Topic details

Introduction

This topic covers ordering and lookup. You analyse the main sorting algorithms for time, space and stability, understand the O(n log n) comparison-sort lower bound, see when linear-time non-comparison sorts apply, and implement binary search with correct boundary handling.

Key relations & formulas

Formulas (Indian textbook notation)

  • binarysearch:O(logn)onasortedarraybinary search: O(log n) on a sorted array

Formulas (Indian textbook notation)

  • mergesort:T(n)=2T(n2)+O(n)O(nlogn)merge sort: T(n) = 2T(\frac{n}{2}) + O(n) → O(n log n)

Formulas (Indian textbook notation)

  • quicksort:O(nlogn)average,O(n2)worstwithoutgoodpivotsquicksort: O(n log n) average, O(n^{2}) worst without good pivots

Notation and sign conventions

Relation 1 —
binarysearch:Obinary search: O

Formulas (Indian textbook notation)

  • binarysearch:O(logn)onasortedarraybinary search: O(log n) on a sorted array
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
mergesort:Tmerge sort: T

Formulas (Indian textbook notation)

  • mergesort:T(n)=2T(n2)+O(n)O(nlogn)merge sort: T(n) = 2T(\frac{n}{2}) + O(n) → O(n log n)
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
quicksort:Oquicksort: O

Formulas (Indian textbook notation)

  • quicksort:O(nlogn)average,O(n2)worstwithoutgoodpivotsquicksort: O(n log n) average, O(n^{2}) worst without good pivots
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Any sort that only compares keys must make at least log₂(n!) ≈ n log n comparisons, so O(n log n) is optimal for the general case — merge sort guarantees it (at the cost of extra memory and with stability), heap sort achieves it in place but unstably, and quicksort is fastest in practice on average though a bad pivot can degrade it to O(n²). When keys come from a small bounded range, counting and radix sort sidestep the comparison bound to reach O(n). Binary search complements sorting: on a sorted array it discards half the candidates per comparison, but its correctness hinges on careful mid-point and boundary arithmetic to avoid infinite loops.

Assumptions and validity limits

State assumptions explicitly before using any relation for sorting and searching — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Algorithms viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Algorithms papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to sorting and searching.
4. Use equation 1:
binarysearch:Obinary search: O
.
5. Use equation 2:
mergesort:Tmerge sort: T
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Sorting and Searching appears in competitive programming and backend systems. In Indian it software curricula this topic is tested because it connects theory to design and analysis of algorithms.
GATE and semester exams often combine sorting and searching with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use sorting and searching?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students claim quicksort is always O(n log n) (its worst case is O(n²)), forget merge sort’s O(n) extra space, and mislabel which sorts are stable. Off-by-one errors in binary-search boundaries and applying binary search to an unsorted array are frequent.

Quick revision checklist

Before attempting sorting and searching problems, confirm you can:
1. A stable sort preserves the order of equal keys
2. Heap sort is O(n log n) in place but not stable
3. Counting and radix sort reach O(n) when the key range is bounded
Revise the solved examples in Clrs Algorithms — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Binary search iterations

Problem

How many comparisons does binary search need in the worst case for n = 1024?

Solution

Worst case ≈ ⌊log₂ n⌋ + 1 = log₂1024 + 1 = 11, since each step halves the search space of 1024 down to 1.

Conceptual check — Sorting and Searching

Problem

In a Algorithms semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of sorting and searching." What should a complete answer include?

📖 Standard books (India)

  • Clrs AlgorithmsStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus