A recursive binary search (mid = low + (high − low)/2) is run on A = [10, 20, 30, 40, 50, 60, 70, 80, 90]. How many calls are made to find (a) 10 and (b) to decide that 95 is absent?
Searching locates a key in a collection. Linear search works on any list in O(n); binary search halves a sorted array at every step for O(log n); interpolation, jump and exponential search refine the idea, and BSTs and hashing extend it to dynamic data.
Searching means finding the position of a record whose key equals a given search key, or reporting that no such record exists. A search is successful if the key is found and unsuccessful otherwise; both cases matter, because ‘not found’ is often the expensive one. Internal searching works on data in main memory, external searching on data on disk, where the number of block reads dominates.
| Family | Methods | Precondition |
|---|---|---|
| Sequential | linear search, sentinel search, self-organising lists | none |
| Interval (divide the range) | binary, interpolation, jump, exponential, Fibonacci search | sorted array |
| Tree-based | BST, AVL, red–black, B-tree search | data kept in a search tree |
| Hashing | chaining, open addressing | a hash table |
Searching is the reason so much of this module exists: sorting pays for itself by enabling binary search, balanced trees keep searches logarithmic while the data changes, and hashing trades memory for constant average time.
Linear search compares the key with each element in turn, from the first to the last, stopping at the first match. It needs no preparation, works on unsorted data, and is the natural search for a linked list or a sequential file. For small n it is perfectly good.
#include <stdio.h>
int linearSearch(const int a[], int n, int key) { /* index of key, or -1 */
for (int i = 0; i < n; i++)
if (a[i] == key) return i;
return -1;
}
int sentinelSearch(int a[], int n, int key) { /* a[] needs one spare cell a[n] */
a[n] = key; /* the key is now always found */
int i = 0;
while (a[i] != key) i++; /* no separate i < n test */
return (i < n) ? i : -1; /* found the sentinel: absent */
}
int main(void) {
int a[8] = {31, 7, 19, 4, 26, 12, 9}; /* 7 keys + 1 spare cell */
int p = linearSearch(a, 7, 26);
int q = sentinelSearch(a, 7, 9);
int r = sentinelSearch(a, 7, 5);
printf("%d %d %d\n", p, q, r); /* 4 6 -1 */
return 0;
}| Case | Comparisons | When |
|---|---|---|
| Best | 1 | the key is the first element |
| Worst (successful) | n | the key is the last element |
| Unsuccessful | n | every element is examined (n + 1 tests with a sentinel) |
| Average (successful) | (n + 1)/2 | each of the n positions equally likely |
i < n test).Binary search works on a sorted array. Compare the key with the middle element: if they are equal the search is over; if the key is smaller, it can only be in the left half; if larger, only in the right half. Each comparison discards half of the remaining elements. It needs random access to reach the middle in O(1), so it is meant for arrays, not linked lists.
#include <stdio.h>
int binarySearch(const int a[], int n, int key) { /* a[] sorted ascending */
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2; /* never overflows */
if (a[mid] == key) return mid;
if (a[mid] < key) low = mid + 1; /* discard the left half */
else high = mid - 1; /* discard the right half */
}
return -1; /* range empty: absent */
}
int binarySearchRec(const int a[], int low, int high, int key) { /* O(log n) stack */
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (a[mid] == key) return mid;
return (a[mid] < key) ? binarySearchRec(a, mid + 1, high, key)
: binarySearchRec(a, low, mid - 1, key);
}
int main(void) {
int a[] = {3, 8, 15, 21, 27, 33, 42, 50, 61};
int i = binarySearch(a, 9, 42);
int j = binarySearch(a, 9, 10);
int k = binarySearchRec(a, 0, 8, 61);
printf("%d %d %d\n", i, j, k); /* 6 -1 8 */
return 0;
}| Key | Probe | low | high | mid (a[mid]) | Decision |
|---|---|---|---|---|---|
| 42 | 1 | 0 | 8 | 4 (27) | 27 < 42 → low = 5 |
| 42 | 2 | 5 | 8 | 6 (42) | found at index 6 — 2 probes |
| 10 | 1 | 0 | 8 | 4 (27) | 27 > 10 → high = 3 |
| 10 | 2 | 0 | 3 | 1 (8) | 8 < 10 → low = 2 |
| 10 | 3 | 2 | 3 | 2 (15) | 15 > 10 → high = 1 |
| 10 | — | 2 | 1 | — | low > high → not found after 3 probes |
int indices, (low + high)/2 overflows once low + high exceeds 2³¹ − 1 — possible for arrays of more than about a billion elements. This bug sat in Java's Arrays.binarySearch for nine years until Joshua Bloch reported it in 2006. Writing low + (high - low) / 2 is always safe.Each probe does O(1) work and halves the range: T(n) = T(n/2) + 1, so T(n) = Θ(log n). Drawing every possible sequence of probes gives a decision tree — a balanced BST over the array positions whose height is ⌊log₂ n⌋. A search follows one root-to-leaf path, so no search, successful or not, needs more than ⌊log₂ n⌋ + 1 probes.
| n | 7 | 15 | 100 | 1,000 | 1,024 | 4,096 | 10⁶ | 10⁹ |
|---|---|---|---|---|---|---|---|---|
| max probes | 3 | 4 | 7 | 10 | 11 | 13 | 20 | 30 |
When n = 2ᵏ − 1 the decision tree is perfect, so every unsuccessful search takes exactly k probes (31 elements → always 5) and the average successful search is easy to compute: for n = 7, one key is found in 1 probe, two in 2 and four in 3, an average of 17/7 ≈ 2.43 probes. Compare linear search's average of (7 + 1)/2 = 4 — and the gap widens dramatically: for n = 10⁶, about 20 probes against 500,000.
Interpolation search (W. W. Peterson, 1957) probes where the key ought to be if values were spread evenly, instead of always probing the middle. On uniformly distributed keys it averages O(log log n) probes — about 5 for a billion keys. On skewed data it can degrade to O(n), and its arithmetic per probe is costlier than binary search's.
Jump search checks a[m − 1], a[2m − 1], a[3m − 1], … until it reaches an element ≥ key, then scans that block linearly. The worst case costs about n/m jumps plus m − 1 scans, which is smallest when m = √n, giving O(√n). It suits storage where moving forward is cheap but jumping back is costly. Example: n = 16, m = 4; to find 61 in [2, 5, 8, 12, 16, 23, 38, 45, 56, 61, 72, 77, 85, 91, 96, 99] check 12, 45, 77 (77 ≥ 61), then scan 56, 61 — found after 5 comparisons.
Exponential (galloping) search probes positions 1, 2, 4, 8, … until a[2ⁱ] ≥ key, then binary-searches the last interval: O(log i) where i is the key's position, which suits very large or unbounded sorted lists. Fibonacci search splits the range at Fibonacci numbers, using only addition and subtraction to compute probe positions.
#include <stdio.h>
#include <math.h>
int interpolationSearch(const int a[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high && key >= a[low] && key <= a[high]) {
if (a[high] == a[low]) return (a[low] == key) ? low : -1;
int pos = low + (int)((double)(key - a[low]) * (high - low) / (a[high] - a[low]));
if (a[pos] == key) return pos;
if (a[pos] < key) low = pos + 1; else high = pos - 1;
}
return -1;
}
int jumpSearch(const int a[], int n, int key) {
int step = (int)sqrt(n), prev = 0, next = step;
while (next < n && a[next - 1] < key) { prev = next; next += step; } /* jump */
if (next > n) next = n;
for (int i = prev; i < next; i++) /* linear scan inside the block */
if (a[i] == key) return i;
return -1;
}
int main(void) {
int u[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int b[] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 61, 72, 77, 85, 91, 96, 99};
int x = interpolationSearch(u, 10, 70);
int y = jumpSearch(b, 16, 61);
printf("%d %d\n", x, y); /* 6 9 */
return 0;
}| Method | Requires | Best | Average | Worst | Extra space |
|---|---|---|---|---|---|
| Linear | nothing | 1 | (n + 1)/2 → O(n) | n | O(1) |
| Binary | sorted array | 1 | ≈ log₂ n − 1 | ⌊log₂ n⌋ + 1 | O(1) iterative |
| Interpolation | sorted, roughly uniform keys | 1 | O(log log n) | O(n) | O(1) |
| Jump | sorted array | 1 | O(√n) | O(√n) | O(1) |
| Exponential | sorted, possibly unbounded | 1 | O(log i) | O(log n) | O(1) |
| BST search | a binary search tree | 1 | O(log n) if randomly built | O(n) if skewed | the tree |
| AVL / red–black search | a balanced BST | 1 | O(log n) | O(log n) | the tree |
| Hash table | a hash table | 1 | O(1) | O(n) | the table |
Try the problem first, then reveal the working one step at a time.
A recursive binary search (mid = low + (high − low)/2) is run on A = [10, 20, 30, 40, 50, 60, 70, 80, 90]. How many calls are made to find (a) 10 and (b) to decide that 95 is absent?
What is the maximum number of probes binary search needs for n = 100, n = 1,000 and n = 4,096?
Run interpolation search for 70 in [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] and for 9 in [1, 2, 3, 4, 5, 6, 7, 8, 9, 1000]. Compare with binary search.
An unsorted array holds n = 100 distinct keys. A search key is present with probability 0.5, and when present it is equally likely to be at any position. What is the expected number of comparisons for linear search?
For a sorted array of 15 elements, find the average number of probes of a successful binary search, assuming each key is equally likely.
Find the bugs: int low = 0, high = n - 1; while (low < high) { int mid = (low + high) / 2; if (a[mid] == key) return mid; if (a[mid] < key) low = mid; else high = mid - 1; } return -1;
What block size minimises the worst-case number of comparisons of jump search on n = 400 sorted elements, and about how many comparisons is that? Compare with binary search.
Choose a search method for each and justify it: (a) 20 names typed once into an unsorted list; (b) a static telephone directory of 10 million sorted numbers searched thousands of times a second; (c) a symbol table of a compiler with constant insertions and exact-match lookups; (d) an index that must answer ‘all employees with salary between ₹40,000 and ₹60,000’.
10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.