Skip to content
GATEiit

GATE CSE · Algorithms

Searching, sorting and hashing

Linear and binary search, the standard comparison sorts and their best, average and worst cases, linear-time sorts, and hashing with chaining and open addressing, as GATE asks them: counts, complexities and final table states.

Before you start

  • Asymptotic notation →

    Every sorting and searching question is answered as a Θ or O bound.

  • Arrays and linked lists →

    Binary search needs random access; chaining stores each bucket as a linked list.

  • Binary heaps →

    Heap sort is built on build-heap and repeated delete-max.

  • Logarithms

    Search and sort counts such as ⌊log⁡2n⌋+1 and log⁡2n! need quick log arithmetic.

This topic comes before: File organization and indexing (e.g. B and B+ trees)

Concept notes

1. Linear and binary search

Linear search scans elements one by one. It works on any list, sorted or not. Worst case n comparisons, Θ(n).

Binary search works only on a sorted array (random access is needed). Compare the key with the middle element and discard half the range each time.

  • Worst case: ⌊log⁡2n⌋+1 comparisons (three-way compare), so Θ(log⁡n).
  • Best case: 1 comparison (key at the middle).
  • Recurrence: T(n)=T(n/2)+1.
  • On a linked list, binary search loses its advantage: reaching the middle costs Θ(n).

Useful variants: finding the first or last occurrence of a key, or the insertion point, still take Θ(log⁡n). Searching for the minimum in a rotated sorted array is also a binary search.

When to use what: unsorted data searched once → linear search. Many searches on static data → sort once (Θ(nlog⁡n)), then binary search each time.

2. Comparison sorts at a glance

Sort Best Average Worst In-place Stable
Bubble (with early exit) n n2 n2 Yes Yes
Selection n2 n2 n2 Yes No
Insertion n n2 n2 Yes Yes
Merge nlog⁡n nlog⁡n nlog⁡n No (Θ(n) extra) Yes
Quick nlog⁡n nlog⁡n n2 Yes (O(log⁡n) stack on average) No
Heap nlog⁡n nlog⁡n nlog⁡n Yes No

Key facts:

  • Selection sort does at most n−1 swaps, the fewest of all; it always makes n(n−1)/2 comparisons.
  • Insertion sort does exactly one shift per inversion (a pair i<j with ai>aj), so it is fast on nearly sorted input.
  • A stable sort keeps equal keys in their original order.
  • Lower bound: any comparison sort needs at least ⌈log⁡2n!⌉ comparisons in the worst case, which is Ω(nlog⁡n). This comes from the decision tree, which needs n! leaves.

3. Quick sort, merge sort, heap sort and linear-time sorts

Merge sort splits in half, sorts both halves and merges: T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n). Merging sorted lists of sizes m and n takes at most m+n−1 comparisons.

Quick sort partitions around a pivot, then sorts both sides. Partition takes Θ(n).

  • Balanced splits: T(n)=2T(n/2)+n=Θ(nlog⁡n).
  • Worst case: pivot is always the smallest or largest (for example, first-element pivot on sorted input): T(n)=T(n−1)+n=Θ(n2), with n(n−1)/2 comparisons.
  • Any split in a fixed ratio, even 1:9, still gives Θ(nlog⁡n).

Heap sort: build a max-heap in Θ(n), then do n−1 delete-max operations, each O(log⁡n).

Linear-time sorts do not compare keys, so the Ω(nlog⁡n) bound does not apply:

  • Counting sort: keys in 0..k, time Θ(n+k), stable.
  • Radix sort: d digits in base b, stable counting sort per digit: Θ(d(n+b)). Must start from the least significant digit.
  • Bucket sort: uniformly distributed input, Θ(n) on average.

4. Hashing: chaining and open addressing

A hash function h maps a key to one of m slots. The load factor is α=n/m for n keys. Two keys with the same slot collide.

Chaining: each slot holds a linked list. With simple uniform hashing the expected chain length is α, and search takes Θ(1+α) on average. α may exceed 1. Worst case (all keys in one slot) is Θ(n).

Open addressing: all keys live in the table itself, so α≤1. On a collision, probe the sequence h(k,0),h(k,1),…

Method Probe h(k,i) Problem
Linear probing (h′(k)+i) mod m Primary clustering: long runs of filled slots
Quadratic probing (h′(k)+c1i+c2i2) mod m Secondary clustering; may not reach every slot
Double hashing (h1(k)+i h2(k)) mod m Best spread; h2(k) must be non-zero and coprime to m

With uniform hashing, an unsuccessful search makes at most 11−α expected probes. Deletion in open addressing must leave a deleted marker, or later searches stop too early.

0123456789432313532737one cluster: slots 3 to 8
Linear probing, m = 10, h(k) = k mod 10, inserting 43, 23, 13, 27, 37, 53: the keys that hash to 3 form a cluster

Formulas to remember

Binary search, worst-case comparisonsSorted array of n elements
⌊log⁡2n⌋+1
Comparison sort lower bound
⌈log⁡2n!⌉=Ω(nlog⁡n)
Merge sort
T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)
Quick sort worst casen(n−1)/2 comparisons
T(n)=T(n−1)+Θ(n)=Θ(n2)
Merging two sorted lists
≤m+n−1 comparisons
Load factorn keys, m slots
α=nm
Chaining, average search
Θ(1+α)
Open addressing, unsuccessful searchUniform hashing, α<1
E[probes]≤11−α
Double hashing probe
h(k,i)=(h1(k)+i h2(k)) mod m

Common mistakes

  • Using binary search on a linked list and claiming O(log⁡n): finding the middle costs linear time.
  • Saying insertion sort is always Θ(n2): on sorted input it is Θ(n), and its shifts equal the number of inversions.
  • Calling quick sort Θ(n2) on average; that is only its worst case.
  • Forgetting that open addressing needs α≤1 while chaining allows α>1.
  • In linear probing, deleting a key by emptying its slot; later searches stop at the hole and miss keys beyond it.
  • Applying the Ω(nlog⁡n) lower bound to counting or radix sort, which do not compare keys.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Final table after linear probing

numerical

A hash table has 10 slots (0 to 9) and uses h(k)=k mod 10 with linear probing. Insert 43, 23, 13, 27, 37, 53 in that order. Where does each key go, and how many probes does inserting 53 take?

  1. Step 1: Place 43, 23 and 13.

    Try it on paper first.

Example 2: Insertion sort shifts equal inversions

numerical

How many element shifts does insertion sort make on the array [5, 2, 4, 6, 1, 3]?

  1. Step 1: Recall what one shift fixes.

    Try it on paper first.

Example 3: Binary search and the sorting lower bound

numerical

(a) What is the worst-case number of comparisons for binary search in a sorted array of 1000 elements? (b) What is the minimum worst-case number of comparisons any comparison sort needs for 5 elements?

  1. Step 1: Apply the binary search formula.

    Try it on paper first.

Example 4: Choosing a sort for the constraints

conceptual

You need a sort that is in-place and guarantees O(nlog⁡n) time in the worst case. Which standard sort fits, and why do merge sort and quick sort not?

  1. Step 1: Check merge sort.

    Try it on paper first.

Previous year questions

16 official GATE CSE questions on this topic, newest first.

Practise these 16 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.