Sorting & hashing formulas
Searching, sorting and hashing. 15 formulas for GATE CS, DA Data Structures and Algorithms, each with when to use it. Each is explained in the topic notes.
IAE Academy: Searching, sorting and hashing formulas (GATE CS, DA)
All 15 Sorting & hashing formulas
LINEAR SEARCH: NUMBER OF KEY COMPARISONS
The successful-search average assumes the key is present and equally likely to be at each of the positions. Both the worst case and the average case are .
BINARY SEARCH COMPLEXITY
Convention: each probe of a[mid] counts as one (three-way) comparison. The worst case holds for both successful and unsuccessful search. If a question counts the == test and the < test as separate comparisons, recount under its rule. Valid only for a sorted array with random access.
FINDING THE MINIMUM AND MAXIMUM TOGETHER
Method: take the elements in pairs. Compare the two elements of each pair with each other, then the smaller with the current minimum and the larger with the current maximum. That is 3 comparisons per pair instead of 4. Finding only the maximum (or only the minimum) takes exactly comparisons. Both counts are the best possible for comparison-based algorithms.
LOWER BOUND FOR COMPARISON-BASED SORTING
Any sort that learns about the keys only by comparing pairs of them must make comparisons in the worst case, and also on average. Proof idea: there are possible orderings of distinct keys. A binary decision tree that distinguishes them all has at least leaves, so its height is at least , and by Stirling's approximation.
NUMBER OF INVERSIONS
The maximum occurs for reverse-sorted input. The expected value is for a uniformly random permutation of distinct keys, since each of the pairs is inverted with probability . Insertion sort runs in time, so it is fast on nearly sorted input.
BALANCED DIVIDE-AND-CONQUER RECURRENCE (MERGE SORT)
This applies whenever a problem of size is split into 2 equal halves with linear combine cost. The recursion tree has levels, each costing in total.
QUICKSORT RECURRENCES
Even a lopsided but constant-fraction split, such as 1 : 9, gives . Only a split that leaves a constant number of elements on one side at every level gives .
COMPARISONS TO MERGE TWO SORTED LISTS
This is for the standard two-pointer merge of sorted lists of sizes and . The worst case occurs when the lists interleave. The best case occurs when every element of the shorter list is smaller than every element of the longer one.
BINARY HEAP OPERATION COSTS (HEAP OF N ELEMENTS)
Bottom-up build-heap is , not , because most nodes sit near the bottom and sift down only a short distance. Heap sort is build-heap followed by extract-max operations: .
COUNTING SORT COMPLEXITY
= number of elements; the keys are integers in . The sort is linear when and degrades when is much larger than . It is stable when the output is filled by scanning the input from right to left, as below. It is not in-place.
RADIX SORT COMPLEXITY
= number of digits (passes) and = the range of one digit ( for decimal digits, for bits). Least-significant-digit radix sort applies a stable sort, usually counting sort, once per digit. The total is linear when is a constant and . It is not in-place.
LOAD FACTOR
= number of keys stored, = number of slots. With chaining, can exceed 1. With open addressing every key needs its own slot, so . The open-addressing formulas below need , i.e. at least one free slot.
EXPECTED PROBES: SEPARATE CHAINING (SIMPLE UNIFORM HASHING, NEW KEYS INSERTED AT THE HEAD OF THEIR CHAIN)
These count the keys examined. An unsuccessful search walks the whole chain, whose expected length is . A successful search examines the target plus the keys inserted into the same chain after it. Including the hash computation, both searches take time. In the worst case, when all keys hash to one slot, a search takes .
EXPECTED PROBES: OPEN ADDRESSING UNDER UNIFORM HASHING (EVERY PROBE SEQUENCE EQUALLY LIKELY)
These are upper bounds, valid for . Uniform hashing is an idealisation that double hashing approximates well. Linear and quadratic probing do worse because of clustering.
EXPECTED PROBES: LINEAR PROBING (APPROXIMATIONS FOR LARGE TABLES)
Valid for . The squared term in the unsuccessful case shows how quickly primary clustering hurts as the table fills up.
Where this was asked: 2020 (1), 2022 (1), 2023 (1), 2024 (3), 2025 (7), 2026 (5). See the 18 previous-year questions on Sorting & hashing. This shows what was asked, not what will be asked.