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.
Linear search scans elements one by one. It works on any list, sorted or not. Worst case comparisons, .
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: comparisons (three-way compare), so .
Best case: 1 comparison (key at the middle).
Recurrence: .
On a linked list, binary search loses its advantage: reaching the middle costs .
Useful variants: finding the first or last occurrence of a key, or the insertion point, still take . 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 (), then binary search each time.
2. Comparison sorts at a glance
Sort
Best
Average
Worst
In-place
Stable
Bubble (with early exit)
Yes
Yes
Selection
Yes
No
Insertion
Yes
Yes
Merge
No ( extra)
Yes
Quick
Yes ( stack on average)
No
Heap
Yes
No
Key facts:
Selection sort does at most swaps, the fewest of all; it always makes comparisons.
Insertion sort does exactly one shift per inversion (a pair with ), 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 comparisons in the worst case, which is . This comes from the decision tree, which needs leaves.
3. Quick sort, merge sort, heap sort and linear-time sorts
Merge sort splits in half, sorts both halves and merges: . Merging sorted lists of sizes and takes at most comparisons.
Quick sort partitions around a pivot, then sorts both sides. Partition takes .
Balanced splits: .
Worst case: pivot is always the smallest or largest (for example, first-element pivot on sorted input): , with comparisons.
Any split in a fixed ratio, even , still gives .
Heap sort: build a max-heap in , then do delete-max operations, each .
Linear-time sorts do not compare keys, so the bound does not apply:
Counting sort: keys in , time , stable.
Radix sort: digits in base , stable counting sort per digit: . Must start from the least significant digit.
Bucket sort: uniformly distributed input, on average.
4. Hashing: chaining and open addressing
A hash function maps a key to one of slots. The load factor is for 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 on average. may exceed 1. Worst case (all keys in one slot) is .
Open addressing: all keys live in the table itself, so . On a collision, probe the sequence
Method
Probe
Problem
Linear probing
Primary clustering: long runs of filled slots
Quadratic probing
Secondary clustering; may not reach every slot
Double hashing
Best spread; must be non-zero and coprime to
With uniform hashing, an unsuccessful search makes at most expected probes. Deletion in open addressing must leave a deleted marker, or later searches stop too early.
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 elements
Comparison sort lower bound
Merge sort
Quick sort worst case comparisons
Merging two sorted lists
Load factor keys, slots
Chaining, average search
Open addressing, unsuccessful searchUniform hashing,
Double hashing probe
Common mistakes
Using binary search on a linked list and claiming : finding the middle costs linear time.
Saying insertion sort is always : on sorted input it is , and its shifts equal the number of inversions.
Calling quick sort on average; that is only its worst case.
Forgetting that open addressing needs while chaining allows .
In linear probing, deleting a key by emptying its slot; later searches stop at the hole and miss keys beyond it.
Applying the 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 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?
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]?
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?
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 time in the worst case. Which standard sort fits, and why do merge sort and quick sort not?
Step 1: Check merge sort.
Try it on paper first.
Previous year questions
16 official GATE CSE questions on this topic, newest first.
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.