Chapter 4: Searching Algorithms
Introduction
Learning Outcomes
Implement linear and binary search (binary both iteratively and recursively) in Java, trace them on an array, and work out their best, average and worst cases.
Explain hash functions, buckets, collisions, linear probing and chaining; explain how the load factor affects cost.
Justify the idea, code and complexity of interpolation, jump and exponential search: O(log log n), O(√n), O(log i).
Choose a search algorithm based on whether the data is sorted, its distribution and its size; avoid common implementation mistakes.
Introduction
The Search Problem
- Search algorithms find a desired value in a collection of data.
- They scan the data by a search criterion (usually a key) and try to find a matching item.
arr of n items and a target value.arr[i] == target; −1 if the target is absent (unsuccessful search).Introduction
Kinds of Search Algorithms
Search algorithms differ in how they work and in what they require.
| Algorithm | Requires | How it works |
|---|---|---|
| Linear search | nothing | Compares the items one by one. |
| Binary search | sorted array | Halves the search range at each step. |
| Hash search | hash table | Items are stored in a hash table; the search looks in the key's bucket. |
| Interpolation search | sorted, uniform | Estimates the position from the ratio of values. |
| Jump search | sorted array | Jumps ahead in blocks of √n, then searches linearly inside a block. |
| Exponential search | sorted array | Grows a bound as 1, 2, 4, 8, …, then runs binary search. |
Linear Search
Linear Search
- Searches by checking every item in turn.
- Like checking the items on a shopping list in order, or searching a room systematically for something you lost.
- The target is compared with each item, starting from the beginning of the list:
- if found, its position (index) is returned;
- if absent, the failure value −1 is returned.
- Sometimes looking one by one is the best way to find something: if the array is unsorted there is no other option.
Linear Search
The Linear Search Algorithm
int linearSearch(int[] arr, int n, int target) { int i = 0; while (i < n && arr[i] != target) i++; if (i < n) return i; // found: index else return -1; // not found }
- Parameters:
arrthe items to search,nthe array size,targetthe item to find. - Returns: the index of the target; −1 if not found.
iwalks over the array; thewhileloop continues while i < n and arr[i] ≠ target, incrementing i each time.- After the loop, the
ifchecks whether the item was found: if i < n it returns i (the item's index), otherwise −1.
i < n is tested first, and thanks to Java's short-circuit &&, arr[n] is never accessed when i == n.Linear Search · Step by step
Linear Search: Best and Worst Case
Best case: search for 33. The array is unsorted: 33, 21, 12, 7, 8, 1, 3, 44, 56, 18 (n = 10).
arr[0] = 33 = target → the loop ends at once; returns 0. Only 1 comparison: best case O(1).
Worst case: search for 17. 17 is not in the array; every item must be checked.
i = 0: arr[0] = 33 ≠ 17 → i = 1.
i = 1: arr[1] = 21 ≠ 17 → i = 2.
i = 2: arr[2] = 12 ≠ 17 → i = 3.
i = 3: arr[3] = 7 ≠ 17 → i = 4.
i = 4: arr[4] = 8 ≠ 17 → i = 5.
i = 5: arr[5] = 1 ≠ 17 → i = 6.
i = 6: arr[6] = 3 ≠ 17 → i = 7.
i = 7: arr[7] = 44 ≠ 17 → i = 8.
i = 8: arr[8] = 56 ≠ 17 → i = 9.
i = 9: arr[9] = 18 ≠ 17 → i = 10.
i = 10 = n → the loop ends; i < n is false, so it returns −1 (not found). n = 10 comparisons: worst case O(n).
Linear Search · Run the code step by step
linearSearch(arr, 10, 1): Tracing the Code
Start with i = 0. Target: 1 (an average case example).
i < n and arr[0] = 33 ≠ 1 → the loop body runs.
i++ → i = 1.
i < n and arr[1] = 21 ≠ 1 → the loop body runs.
i++ → i = 2.
i < n and arr[2] = 12 ≠ 1 → the loop body runs.
i++ → i = 3.
i < n and arr[3] = 7 ≠ 1 → the loop body runs.
i++ → i = 4.
i < n and arr[4] = 8 ≠ 1 → the loop body runs.
i++ → i = 5.
arr[5] = 1 = target → the while condition is false and the loop exits.
i = 5 < n → returns 5. 6 comparisons were made.
int linearSearch(int[] arr, int n, int target) { int i = 0; while (i < n && arr[i] != target) i++; if (i < n) return i; else return -1; }
Linear Search
Linear Search: Complexity and Assessment
| Case | Comparisons | Order | Example (n = 10) |
|---|---|---|---|
| Best (first item) | 1 | O(1) | 33 → 1 comparison |
| Average (successful, equally likely) | (1 + 2 + … + n) / n = (n + 1)/2 | O(n) | average 5.5 |
| Worst / unsuccessful | n | O(n) | 17 → 10 comparisons |
Advantages
- Easy to write and understand; needs no complex data structure.
- No preprocessing: the array need not be sorted.
- Ideal for small data sets (e.g. a number in a contact list).
- Also works on sequential-access structures such as linked lists.
Disadvantages
- Slow on large data: 500,000 comparisons on average for 10⁶ items.
- Even if the array is sorted, it does not exploit that.
- With many queries, sorting once and using binary search is cheaper.
Linear Search
Linear Search with a Sentinel
int sentinelSearch(int[] arr, int n, int target) { if (n == 0) return -1; int last = arr[n - 1]; arr[n - 1] = target; // sentinel int i = 0; while (arr[i] != target) i++; arr[n - 1] = last; // restore if (i < n - 1 || last == target) return i; return -1; }
- The classic loop makes two comparisons per iteration:
i < nandarr[i] != target. - If the target is written into the last cell as a sentinel, the loop is guaranteed to stop; the
i < ntest is no longer needed. - The comparison count drops from ≈ 2n to ≈ n + 2; the order is still O(n).
Linear Search · Run the code step by step
sentinelSearch(arr, 5, 5): Tracing the Code
arr = [7, 3, 9, 4, 6], target = 5 (absent). The last item, 6, is saved in last.
The target 5 is written into the last cell as a sentinel: the loop now stops here at the latest. i = 0.
arr[0] = 7 ≠ 5 → i++. Only one comparison; no i < n test.
arr[1] = 3 ≠ 5 → i++. Only one comparison; no i < n test.
arr[2] = 9 ≠ 5 → i++. Only one comparison; no i < n test.
arr[3] = 4 ≠ 5 → i++. Only one comparison; no i < n test.
arr[4] = 5 = target: the loop stops. But what it found is the sentinel, not a real item.
The last item is restored: arr is [7, 3, 9, 4, 6] again.
i < n − 1? 4 < 4 is false; last == 5? 6 ≠ 5 → return -1. 7 comparisons in total = n + 2; the classic loop would make 2n = 10.
int sentinelSearch(int[] arr, int n, int target) { if (n == 0) return -1; int last = arr[n - 1]; arr[n - 1] = target; // sentinel int i = 0; while (arr[i] != target) i++; arr[n - 1] = last; // restore if (i < n - 1 || last == target) return i; return -1; }
Binary Search
Binary Search
- Like looking for a book in a library whose books are shelved alphabetically by the author's surname.
- Instead of searching every shelf, go to the middle first.
- If the book is to the left of the middle, continue on the left shelves;
- if it is to the right, continue on the right shelves.
- Each comparison halves the part left to search.
- Precondition: the array must be sorted, and items must be accessible by index in O(1).
Binary Search
Why Is Binary Search Fast?
- It is fast on sorted lists: the remaining part halves at every step.
- On a list of 1000 items, linear search makes 500 checks on average, while binary search finishes in at most 10 steps.
- 1000 → 500 → 250 → 125 → 62 → 31 → 15 → 7 → 3 → 1: one comparison per range size, 10 in total.
- Ideal for searching large, sorted data sets.
| n | Linear (worst) | Binary (worst) |
|---|---|---|
| 10 | 10 | 4 |
| 1,000 | 1,000 | 10 |
| 10⁶ | 10⁶ | 20 |
| 10⁹ | 10⁹ | 30 |
Binary Search
The Binary Search Algorithm
int binarySearch(int[] arr, int n, int target) { int lo = 0, hi = n - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (target < arr[mid]) hi = mid - 1; // left half else if (target > arr[mid]) lo = mid + 1; // right half else return mid; // found } return -1; }
loandhibound the range being searched.- The
whileloop continues as long aslo ≤ hi(the range is not empty). midis the index of the middle item of the range at each step.- If the target is smaller than the middle, continue in the lower half; if larger, in the upper half; if equal, return
mid. - If the range becomes empty (
lo > hi), return −1.
Binary Search
Recursive Binary Search
int search(int[] arr, int lo, int hi, int target) { if (lo > hi) return -1; // empty range int mid = (lo + hi) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) // right half return search(arr, mid + 1, hi, target); return search(arr, lo, mid - 1, target); }
- The initial call is
search(arr, 0, n - 1, target); the call replaces the loop's update oflo/hi. - Viewed as divide and conquer: divide at the middle item, conquer by continuing in only one half, no combine.
- Since it has a single branch, it is also called decrease and conquer: T(n) = T(n/2) + 1.
- Extra memory: an O(log n) call stack (O(1) in the iterative version).
Binary Search · Step by step
Binary Search = Balanced Binary Search Tree
A sorted array of 15 items. Let us build the best (balanced) binary search tree from it.
The middle item 21 becomes the root: it is always the first comparison of binary search.
The middle of the left half is 14, of the right half 28: items smaller than 21 go left, larger ones right.
The middle of each subrange forms the next level: 11, 18, 25, 32.
The remaining items are leaves. The tree has height 3 and 4 levels = ⌊log₂ 15⌋ + 1: every search makes at most 4 comparisons.
Binary Search · Step by step
A Sorted Array Is an Implicit Tree
A second sorted array (15 items: 1, 3, 5, 6, …, 25, 27) can be viewed as a tree in the same way. The tree is never built in memory: the mid computation walks it implicitly. Let us search for 20.
mid = 7 → arr[7] = 14; 20 > 14 → go right.
mid = 11 → arr[11] = 22; 20 < 22 → go left.
mid = 9 → arr[9] = 18; 20 > 18 → go right.
mid = 10 → arr[10] = 20: found. 4 comparisons from root to leaf; path 14 → 22 → 18 → 20.
Binary Search · Step by step
Binary Search: Best and Worst Case
Best case: search for 23. lo = 0, hi = 8.
mid = (0 + 8) / 2 = 4 → arr[4] = 23 = target: found in 1 step, returns 4.
Worst case: search for 54 (absent).
Step 1: mid = 4 → 23 < 54 → lo = 5; the left half is discarded.
Step 2: mid = (5 + 8) / 2 = 6 → 55 > 54 → hi = 5.
Step 3: mid = (5 + 5) / 2 = 5 → 34 < 54 → lo = 6.
lo = 6 > hi = 5: the range is empty → not found in 3 steps, returns −1. If 54 were present it would lie between 34 and 55 (at index 6).
Binary Search · Run the code step by step
binarySearch(arr, 9, 47): Tracing the Code
Target 47; lo = 0, hi = 8.
lo ≤ hi → mid = (0 + 8) / 2 = 4; arr[4] = 14.
47 > 14 → lo = 5; the left part is discarded.
lo ≤ hi → mid = (5 + 8) / 2 = 6; arr[6] = 45.
47 > 45 → lo = 7; the left part is discarded.
lo ≤ hi → mid = (7 + 8) / 2 = 7; arr[7] = 47.
47 == arr[7] → returns 7. 3 comparisons on 9 items (at most ⌊log₂ 9⌋ + 1 = 4).
int binarySearch(int[] arr, int n, int target) { int lo = 0, hi = n - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (target < arr[mid]) hi = mid - 1; // left half else if (target > arr[mid]) lo = mid + 1; // right half else return mid; // found } return -1; }
Binary Search
Binary Search: Complexity
T(n) = T(n/2) + 1 ⇒ T(n) = O(log n)
- The range at least halves on every iteration: n → n/2 → n/4 → … → 1.
- After k iterations n/2ᵏ ≤ 1: k ≈ log₂ n.
- Extra memory: O(1) for the iterative version, an O(log n) stack for the recursive one.
| Case | Order |
|---|---|
| Best (first mid matches) | O(1) |
| Average | O(log n) |
| Worst / unsuccessful | O(log n) |
| Preprocessing (sorting) | O(n log n), once |
Binary Search
Binary Search: Implementation Details
(lo + hi) / 2 goes negative when lo + hi > 2³¹ − 1. Safe forms: lo + (hi - lo) / 2 or (lo + hi) >>> 1.hi = mid or lo = mid may not shrink the range → infinite loop. The middle item has already been checked, so the ±1 is required.hi = mid − 1.Arrays.binarySearch(arr, x): if not found it returns −(insertion point) − 1, which also tells where x can be inserted without breaking the order.Binary Search · Run the code step by step
First Position Among Duplicates: firstIndex(arr, 4)
arr = [2, 4, 4, 4, 4, 7, 9, 12], target = 4: it occurs four times. The goal is the index of the first 4. Start with result = −1.
mid = 0 + (7 − 0) / 2 = 3: arr[3] = 4 = target. Instead of returning, record result = 3 and look for an earlier 4 on the left with hi = 2.
mid = 0 + (2 − 0) / 2 = 1: arr[1] = 4 = target. Instead of returning, record result = 1 and look for an earlier 4 on the left with hi = 0.
mid = 0: arr[0] = 2 < 4 → lo = 1.
lo = 1 > hi = 0: the range is empty and the loop ends. return 1: the first 4 is at index 1. Still O(log n): 3 probes.
int firstIndex(int[] arr, int target) { int lo = 0, hi = arr.length - 1, result = -1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (arr[mid] == target) { result = mid; // record, hi = mid - 1; // go left } else if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; } return result; }
Hashing
Hash Table Search
- Like a key that fits its lock.
- The data consists of keys and their associated values.
- A hash function turns the key into a table index (the hash value), which points to the data's place in the table.
- The search looks in the slot for the key's hash value (the bucket).
- If the bucket is not empty, the key is compared with the keys in it; a match ends the search.
Hashing
Hashing: Pros and Cons
Advantages
- Very fast search on average: O(1).
- The data need not be sorted in advance.
- Ideal for searching large data sets.
- With a well-chosen hash function, the target is accessed directly.
Disadvantages
- The hash function can cause collisions: two different keys with the same hash value.
- Collisions need extra steps; in the worst case O(n).
- Needs extra memory (empty buckets); no ordered traversal, minimum or range queries.
Hashing
The Hash Function
int hash(String key, int m) { int sum = 0; for (char k : key.toCharArray()) sum += k; // Unicode code return sum % m; // m: table size }
| key | character codes | sum % 10 |
|---|---|---|
| "ali" | 97 + 108 + 105 = 310 | 0 |
| "ila" | 105 + 108 + 97 = 310 | 0 |
| "veli" | 118 + 101 + 108 + 105 = 432 | 2 |
- This function adds up the Unicode codes of the characters (the numeric value of a Java
char) and takes the result mod the table size. - Simple but weak: words with the same letters ("ali" and "ila") always collide.
- A good hash function is fast to compute and spreads keys evenly over the buckets; Java's
String.hashCode()weights each position: Σ s[i] · 31ⁿ⁻¹⁻ⁱ.
Hashing · Run the code step by step
hash("veli", 10): Tracing the Code
key = "veli", m = 10 buckets. sum = 0.
k = 'v': add its Unicode code 118 → sum = 118.
k = 'e': add its Unicode code 101 → sum = 219.
k = 'l': add its Unicode code 108 → sum = 327.
k = 'i': add its Unicode code 105 → sum = 432.
432 % 10 = 2: "veli" lands in bucket 2. "ali" and "ila" have the same letters, so both sum to 310 and collide in bucket 0.
int hash(String key, int m) { int sum = 0; for (char k : key.toCharArray()) sum += k; // Unicode code return sum % m; // m: table size }
Hashing
Hash Table Search (Linear Probing)
int hashSearch(Integer[] table, int key) { int m = table.length; // m > 0 int idx = Math.floorMod(key, m); // hash while (table[idx] != null) { if (table[idx] == key) return idx; // found idx = (idx + 1) % m; // next bucket } return -1; // empty: absent }
- Data structures:
tablestores the keys (key–value pairs in general); the hash function turns a key into an index. - The
whileloop runs untiltable[idx]is empty (null). - Each iteration compares the key in the bucket with the target; on a match it returns the position (the value, in a key–value table).
floorMod: the index stays in 0..m−1 even for negative keys.- With no match, the next bucket is checked;
% mwraps around from the end to the start (circular search). - Reaching an empty bucket means the key is absent.
Hashing · Step by step
Insertion with Linear Probing
Empty table (m = 10). Keys are inserted in order: 16, 12, 45, 29, 6, 122, 5, 58, 75; h(x) = x % 10.
h(16) = 6: bucket empty → 16 goes here.
h(12) = 2: bucket empty → 12 goes here.
h(45) = 5: bucket empty → 45 goes here.
h(29) = 9: bucket empty → 29 goes here.
h(6) = 6 is full → probe 7; first empty bucket 7. (2 probes)
h(122) = 2 is full → probe 3; first empty bucket 3. (2 probes)
h(5) = 5 is full → probe 6, 7, 8; first empty bucket 8. (4 probes)
h(58) = 8 is full → probe 9, 0; first empty bucket 0. (3 probes)
h(75) = 5 is full → probe 6, 7, 8, 9, 0, 1; first empty bucket 1. (7 probes) The full buckets form a contiguous cluster (5, 6, …, 9, 0, …, 3; only bucket 4 is empty): primary clustering.
Hashing · Run the code step by step
hashSearch(table, 75): Tracing the Code
m = 10, idx = 75 % 10 = 5.
table[5] = 45 ≠ 75 → idx = (5 + 1) % 10 = 6.
table[6] = 16 ≠ 75 → idx = (6 + 1) % 10 = 7.
table[7] = 6 ≠ 75 → idx = (7 + 1) % 10 = 8.
table[8] = 5 ≠ 75 → idx = (8 + 1) % 10 = 9.
table[9] = 29 ≠ 75 → idx = (9 + 1) % 10 = 0. Wrapped around to the start.
table[0] = 58 ≠ 75 → idx = (0 + 1) % 10 = 1.
table[1] = 75 → returns 1. 7 probes: 75 was reached at the end of the cluster.
int hashSearch(Integer[] table, int key) { int m = table.length; // m > 0 int idx = Math.floorMod(key, m); // hash while (table[idx] != null) { if (table[idx] == key) return idx; // found idx = (idx + 1) % m; // next bucket } return -1; // empty: absent }
Hashing · Step by step
Insertion and Search with Chaining
Separate chaining: each bucket holds the head of a linked list. Same keys, h(x) = x % 10.
h(16) = 6 → bucket 6 is empty; 16 is appended to the end of the list.
h(12) = 2 → bucket 2 is empty; 12 is appended to the end of the list.
h(45) = 5 → bucket 5 is empty; 45 is appended to the end of the list.
h(29) = 9 → bucket 9 is empty; 29 is appended to the end of the list.
h(6) = 6 → bucket 6 is occupied (collision); 6 is appended to the end of the list.
h(122) = 2 → bucket 2 is occupied (collision); 122 is appended to the end of the list.
h(5) = 5 → bucket 5 is occupied (collision); 5 is appended to the end of the list.
h(58) = 8 → bucket 8 is empty; 58 is appended to the end of the list.
h(75) = 5 → bucket 5 is occupied (collision); 75 is appended to the end of the list.
Search for 75: walk the list of bucket 5: 45 ≠ 75, 5 ≠ 75, 75 = 75 → found (3 comparisons). Fewer than the 7 probes of linear probing.
Hashing
Load Factor and Complexity
load factor α = n / m (n: number of keys, m: number of buckets)
| Method | Successful search (avg.) | Unsuccessful search (avg.) | α = 0.5 | α = 0.9 |
|---|---|---|---|---|
| Chaining | ≈ 1 + α/2 | ≈ 1 + α | 1.25 / 1.5 | 1.45 / 1.9 |
| Linear probing | ≈ ½ (1 + 1/(1 − α)) | ≈ ½ (1 + 1/(1 − α)²) | 1.5 / 2.5 | 5.5 / 50.5 |
- If α is kept bounded (e.g. doubling the table and rehashing once α exceeds 0.75), the average search is O(1).
- Worst case O(n): if every key lands in the same bucket (a bad hash function), the search degenerates into linear search.
- With linear probing, clustering drives the cost up fast as α → 1; if the table is completely full, an unsuccessful search never terminates.
The formulas are Knuth's approximations under the uniform hashing assumption.
Interpolation Search
Interpolation Search
- Needs sorted data.
- It assumes the values are spread evenly over the list: after the first value, consecutive items differ by about the same amount.
- Under this assumption, the position of the target is estimated from the smallest and largest items.
- The estimated position is checked; if wrong, the search continues in a narrower range (to the right or left).
- Like opening a phone book near the end, not the middle, to find "Yılmaz".
idx = lo + (target − arr[lo]) · (hi − lo) / (arr[hi] − arr[lo])
Binary search always looks at the middle (ratio ½); interpolation search computes the ratio from the values.
Interpolation Search
The Interpolation Search Algorithm
int interpolationSearch(int[] arr, int n, int target) { int lo = 0, hi = n - 1; while (lo <= hi && target >= arr[lo] && target <= arr[hi]) { if (arr[hi] == arr[lo]) return lo; // avoid /0 long num = (long) (target - arr[lo]) * (hi - lo); int idx = lo + (int) (num / (arr[hi] - arr[lo])); if (arr[idx] == target) return idx; if (arr[idx] < target) lo = idx + 1; else hi = idx - 1; } return -1; }
loandhibound the search range; ifarr[hi] == arr[lo], the target equals that value (avoiding division by 0).- If the target is smaller or larger than the range's end values, the loop is never entered and −1 is returned.
- The estimated
idxis computed and checked:- if equal, return idx;
- if
arr[idx]is smaller, continue fromidx + 1, - if larger, continue up to
idx − 1.
- If the range becomes empty, return −1.
Interpolation Search · Run the code step by step
interpolationSearch(arr, 15, 24): Tracing the Code
Target 24. Bar heights show the values: the distribution is roughly uniform.
idx = 0 + (24 − 10)·14 / (47 − 10) = 0 + 196/37 → 5; arr[5] = 19.
19 < 24 → lo = 6.
idx = 6 + (24 − 20)·8 / (47 − 20) = 6 + 32/27 → 7; arr[7] = 21.
21 < 24 → lo = 8.
idx = 8 + (24 − 22)·6 / (47 − 22) = 8 + 12/25 → 8; arr[8] = 22.
22 < 24 → lo = 9.
idx = 9 + (24 − 23)·5 / (47 − 23) = 9 + 5/24 → 9; arr[9] = 23.
23 < 24 → lo = 10.
idx = 10 + (24 − 24)·4 / (47 − 24) = 10 + 0/23 → 10; arr[10] = 24. Equal → returns 10 (5 probes).
int interpolationSearch(int[] arr, int n, int target) { int lo = 0, hi = n - 1; while (lo <= hi && target >= arr[lo] && target <= arr[hi]) { if (arr[hi] == arr[lo]) return lo; // avoid /0 long num = (long) (target - arr[lo]) * (hi - lo); int idx = lo + (int) (num / (arr[hi] - arr[lo])); if (arr[idx] == target) return idx; if (arr[idx] < target) lo = idx + 1; else hi = idx - 1; } return -1; }
Interpolation Search
Interpolation Search: Complexity and the Bad Case
| Case | Order |
|---|---|
| Best | O(1) |
| Average (uniform distribution) | O(log log n) |
| Worst (skewed distribution) | O(n) |
| Extra memory | O(1) |
- For n = 10⁶, log₂ log₂ n ≈ 4.3; binary search needs ≈ 20 probes.
- Each probe costs more than in binary search (a multiplication and a division).
- The intermediate product
(target − arr[lo]) · (hi − lo)can overflow anintfor large values, so the code computes it as along.
Jump Search
Jump Search
- Works like a hurdler clearing hurdles along the track; the array must be sorted.
- The list is divided into blocks of a fixed size (m = √n).
- Jump one block ahead and check the item there (the last item of the block).
- If the target is there, the search ends; if the target is larger, keep jumping.
- If the value lies in the block just jumped over, go back and search that range linearly.
Jump Search
The Jump Search Algorithm
int jumpSearch(int[] arr, int target, int n) { // n ≤ arr.length if (n == 0) return -1; int block = (int) Math.sqrt(n); int jump = block, pos = 0; while (arr[Math.min(jump, n) - 1] < target) { pos = jump; jump += block; // jump if (pos >= n) return -1; } while (arr[pos] < target) { // linear pos++; if (pos == Math.min(jump, n)) return -1; } if (arr[pos] == target) return pos; return -1; }
- The jump size is computed with
Math.sqrt(n);posstarts at 0. - The first loop looks at the last item of each block, jumping a block as long as it is smaller than the target.
- When the loop ends, the target can only be in the range
[pos, jump). - The second loop scans this range linearly.
- Returns the index if found, otherwise −1.
Jump Search · Run the code step by step
jumpSearch(arr, 69, 10): Tracing the Code
Target 69; n = 10 → block = ⌊√10⌋ = 3; jump = 3, pos = 0.
arr[min(3, 10) − 1] = arr[2] = 30 < 69 → jump a block: pos = 3, jump = 6.
arr[min(6, 10) − 1] = arr[5] = 47 < 69 → jump a block: pos = 6, jump = 9.
arr[8] = 79 ≥ 69 → jumping stops. The target can only be in block [6, 9).
Linear search: arr[6] = 61 < 69 → pos = 7.
arr[7] = 69 → returns 7. Probed: 2, 5, 8, 6, 7.
int jumpSearch(int[] arr, int target, int n) { // n ≤ arr.length if (n == 0) return -1; int block = (int) Math.sqrt(n); int jump = block, pos = 0; while (arr[Math.min(jump, n) - 1] < target) { pos = jump; jump += block; // jump if (pos >= n) return -1; } while (arr[pos] < target) { // linear pos++; if (pos == Math.min(jump, n)) return -1; } if (arr[pos] == target) return pos; return -1; }
Jump Search
Jump Search: Why √n?
worst-case probes ≈ n/m + (m − 1)
- n/m: the maximum number of jumps; m − 1: the linear search inside a block.
- Derivative: −n/m² + 1 = 0 ⇒ m = √n; total ≈ 2√n − 1.
- Complexity O(√n): between linear O(n) and binary O(log n).
- Extra memory O(1); the array must be sorted.
| n | m = √n | ≈ 2√n − 1 | binary |
|---|---|---|---|
| 100 | 10 | 19 | 7 |
| 10,000 | 100 | 199 | 14 |
| 10⁶ | 1,000 | 1,999 | 20 |
Once the block is found, it can be scanned backward from its end (8 → 7) or forward from its start (6 → 7) as in jumpSearch. Either way it takes at most m − 1 extra probes.
Exponential Search
Exponential Search
- Splits the sorted list into exponentially growing pieces: the bound doubles as 1, 2, 4, 8, 16, ….
- Stop once
arr[bound]exceeds the target: the target lies in [bound/2, bound]. - Run binary search on this range (Bentley and Yao, 1976).
- For example, on a list of 1024 items with the target near index 600, the bounds are 1, 2, …, 512, 1024, and binary search continues between 512 and 1023.
Exponential Search
The Exponential Search Algorithm
int exponentialSearch(int[] arr, int target, int n) { if (n == 0) return -1; if (arr[0] == target) return 0; int bound = 1; while (bound < n && arr[bound] <= target) bound *= 2; // 1, 2, 4, 8, ... int lo = bound / 2, hi = Math.min(bound, n - 1); while (lo <= hi) { // binary search int mid = (lo + hi) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; } return -1; }
boundstarts at 1 and is doubled as long asarr[bound] ≤ target.- When the loop ends,
boundmarks the end of the range that can contain the target;bound / 2is its start. Math.min(bound, n − 1): the bound must not run past the array.- The remaining part is searched with binary search.
bound / 2 onward) instead of binary search, the cost would be O(i), and an absent target would scan to the end of the array. Binary search brings the cost down to O(log i).Exponential Search · Run the code step by step
exponentialSearch(arr, 47, 10): Tracing the Code
Target 47. First, arr[0] = 2 ≠ 47.
arr[1] = 9 ≤ 47 → bound = 2.
arr[2] = 30 ≤ 47 → bound = 4.
arr[4] = 38 ≤ 47 → bound = 8.
arr[8] = 79 > 47 → doubling stops. The target lies in [8/2, min(8, 9)] = [4, 8].
Binary search: mid = (4 + 8) / 2 = 6 → arr[6] = 61 > 47 → hi = 5.
Binary search: mid = (4 + 5) / 2 = 4 → arr[4] = 38 < 47 → lo = 5.
Binary search: mid = (5 + 5) / 2 = 5 → arr[5] = 47 = 47 → returns 5.
int exponentialSearch(int[] arr, int target, int n) { if (n == 0) return -1; if (arr[0] == target) return 0; int bound = 1; while (bound < n && arr[bound] <= target) bound *= 2; // 1, 2, 4, 8, ... int lo = bound / 2, hi = Math.min(bound, n - 1); while (lo <= hi) { // binary search int mid = (lo + hi) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; } return -1; }
Exponential Search
Exponential Search: Complexity
- If the target is at index i, the doubling phase takes ⌊log₂ i⌋ + 1 steps.
- The final range [i/2, i] has at most i/2 + 1 items → binary search takes ≈ log₂ i steps.
- Total: O(log i); worst case (i ≈ n) O(log n), best case O(1). Extra memory O(1).
- Faster than binary search when the target is near the front; ≈ 2× slower when it is near the end.
| i (n = 10⁶) | exponential (≈ 2 log₂ i) | binary (≈ log₂ n) |
|---|---|---|
| 10 | ≈ 7 | 20 |
| 1,000 | ≈ 20 | 20 |
| 500,000 | ≈ 38 | 20 |
Comparisons
Comparing the Search Algorithms
| Algorithm | Requires | Best | Average | Worst | Extra memory |
|---|---|---|---|---|---|
| Linear | nothing | O(1) | O(n) | O(n) | O(1) |
| Binary | sorted | O(1) | O(log n) | O(log n) | O(1) |
| Hash (table) | hash table | O(1) | O(1) | O(n) | O(m) |
| Interpolation | sorted, uniform | O(1) | O(log log n) | O(n) | O(1) |
| Jump | sorted | O(1) | O(√n) | O(√n) | O(1) |
| Exponential | sorted | O(1) | O(log i) | O(log n) | O(1) |
i: index of the target, m: number of buckets. The O(1) average of a hash table relies on keeping the load factor bounded. If sorting is needed, add O(n log n) once.
Comparisons
Probe Counts on the Same Array
Array: 2, 9, 30, 32, 38, 47, 61, 69, 79, 81 (the array from the jump and exponential search examples). Cell: number of probes (indices probed).
| target | Linear | Binary | Jump | Interpolation | Exponential | result |
|---|---|---|---|---|---|---|
| 69 | 8 (0,1,2,3,4,5,6,7) | 2 (4,7) | 5 (2,5,8,6,7) | 1 (7) | 7 (0,1,2,4,8,6,7) | 7 |
| 47 | 6 (0,1,2,3,4,5) | 3 (4,7,5) | 5 (2,5,3,4,5) | 1 (5) | 8 (0,1,2,4,8,6,4,5) | 5 |
| 2 | 1 (0) | 3 (4,1,0) | 2 (2,0) | 1 (0) | 1 (0) | 0 |
| 50 | 10 (0,1,2,3,4,5,6,7,8,9) | 4 (4,7,5,6) | 4 (2,5,8,6) | 1 (5) | 8 (0,1,2,4,8,6,4,5) | -1 |
Comparisons
Which Algorithm When?
Comparisons
Search Race: Five Algorithms on One Array
The number above a cell is the probe order; green: found. The table compares the probe counts of the five algorithms for the last target. Interpolation search slows down on the skewed array.
Summary
Summary
| Topic | Key points |
|---|---|
| Linear | compare one by one from start to end; no precondition; average (n + 1)/2, worst n → O(n) |
| Binary | sorted array; compare with the middle, discard half; at most ⌊log₂ n⌋ + 1 → O(log n); the recursive version uses an O(log n) stack |
| Hash | compute the position: h(key); collisions → linear probing or chaining; avg. O(1), worst O(n) |
| Interpolation | estimate the position from the value ratio; O(log log n) on uniform data, O(n) on skewed data |
| Jump | jump in blocks of √n, then search linearly inside the block → O(√n) |
| Exponential | double the bound 1, 2, 4, …, then binary search in [bound/2, bound] → O(log i) |
Summary
Common Mistakes
while (lo < hi) skips a one-item range; hi = mid can cause an infinite loop.(lo + hi) / 2 overflows; lo + (hi − lo) / 2 does not.arr[hi] == arr[lo] it divides by zero; if the target is outside the end values the index leaves the array. Check the range first.Math.min(bound, n − 1) gives an ArrayIndexOutOfBoundsException; a linear scan in the last phase ruins O(log i).In linear probing a deleted bucket is not set to null but marked DELETED; the search skips over it and continues.
Summary
Sample Problems
Array D = 3, 8, 15, 21, 27, 34, 40, 46, 52, 59, 65, 71, 78, 84, 90, 97 (n = 16, indices from 0).
- Which indices does binary search probe for 65?
- Which indices does jump search probe for 65?
- In how many probes does interpolation search settle 65 and 30?
- Which indices does exponential search probe for 65?
- m = 7, h(x) = x % 7, linear probing: insert 10, 24, 17, 31, 5. What is the final table? How many buckets are probed when searching for 12?
Show answers
- mid: 7, 11, 9, 10 → index 10 (4 probes).
- block = 4: block ends 3, 7, 11, then linear 8, 9, 10 → index 10 (6 probes).
- 65: idx 0 + (65 − 3)·15/94 = 9 (59 < 65), then 10 + 0 = 10 → 2 probes. 30: idx 4 (27 < 30) → lo = 5; since 30 < arr[5] = 34 the loop ends → 1 probe, −1.
- 0; 1, 2, 4, 8 (52 ≤ 65), 16 ≥ n → range [8, 15]; mid 11, 9, 10 → index 10 (8 probes).
- [5, —, —, 10, 24, 17, 31] (31 → 3, 4, 5, 6; 5 → 5, 6, 0). 12: h = 5 → 5, 6, 0, 1 (empty) → 4 probes, not found.
Summary
Exercises
- Does ternary search, which splits the range into three parts instead of two (looking at two middle items), make fewer comparisons than binary search?
- Write a binary search that finds the first position of the target in a sorted array with duplicates.
- Show that the average number of comparisons in a successful linear search is (n + 1)/2.
- Show that the best block size for jump search is √n; what is the worst-case probe count for n = 10,000?
- In how many probes does interpolation search find 70 in 10, 20, …, 100? And 9 in 1, 2, …, 9, 1000?
- A chained hash table (m = 10) holds 16, 12, 45, 29, 6, 122, 5, 58, 75. How many keys are compared when searching for 35?
Answers / hints
- No. Each step makes 2 comparisons and keeps 1/3 of the range: T(n) = T(n/3) + 2 → 2 log₃ n ≈ 1.26 log₂ n comparisons, versus log₂ n for binary search. Still O(log n).
- On equality, continue left with
result = mid; hi = mid − 1;and returnresultafter the loop. Still O(log n). - Item i is found with i comparisons, with probability 1/n: Σ i/n = n(n + 1)/(2n) = (n + 1)/2.
- f(m) = n/m + m − 1, f′(m) = −n/m² + 1 = 0 ⇒ m = √n. n = 10,000: 100 + 99 = 199.
- 70: idx = 0 + 60·9/90 = 6 → 1 probe. 9: indices 0, 1, …, 8 → 9 probes (skewed distribution).
- h(35) = 5 → bucket 5 holds 45, 5, 75: 3 comparisons, not found.
Summary
References
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022 (Sections 2.3, 11).
- Sedgewick and Wayne, Algorithms, 4th ed., 2011 (Section 3.1 Binary search, 3.4 Hash tables).
- D. E. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed., 1998.
- J. L. Bentley and A. C. Yao, "An Almost Optimal Algorithm for Unbounded Searching", Information Processing Letters 5(3), 1976.
- Y. Perl, A. Itai and H. Avni, "Interpolation Search — A Log Log N Search", Communications of the ACM 21(7), 1978.
- B. Shneiderman, "Jump Searching: A Fast Sequential Search Technique", Communications of the ACM 21(10), 1978.