Chapter 4: Searching Algorithms

Chapter 4: Searching Algorithms · Study Questions
Linear and sentinel searchBinary searchHash searchInterpolation searchJump searchExponential searchComparison
Sercan KÜLCÜ · Giresun University · Computer Engineering

Output Question · Linear Search

Question 1: How Many Comparisons?

arr = {33, 21, 12, 7, 8, 1, 3, 44, 56, 18} (n = 10): what do linearSearch(arr, 10, 44) and linearSearch(arr, 10, 17) return? In each case, how many times is arr[i] != target evaluated?

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;
}
Show answer
  • 44: found at index 7 → 7 is returned; for i = 0 … 7 that is 8 comparisons.
  • 17: not in the array → −1; 10 comparisons. When i = 10, i < n is false, and thanks to short-circuit && arr[10] is never accessed.

i < n test is also evaluated 8 times for 44 and 11 times for 17. On average a successful search makes (n + 1)/2 = 5.5 comparisons.

Step-by-Step Trace · Linear Search

Question 2: Sentinel Search

sentinelSearch(arr, 5, 7) is called for arr = {12, 5, 9, 20, 7}. Where does the loop stop, and what does the method return?

125920701234sentinel

The target is 7, and the last element is 7 as well. last = 7 is saved and the target (7) is written into the last cell as a sentinel; the array looks unchanged from outside.

125920701234i

arr[0] = 12 ≠ 7 → i = 1. The loop makes only one comparison per step; there is no i < n test.

125920701234i

arr[1] = 5 ≠ 7 → i = 2.

125920701234i

arr[2] = 9 ≠ 7 → i = 3.

125920701234i

arr[3] = 20 ≠ 7 → i = 4.

125920701234i

arr[4] = 7 (the sentinel) → the loop stops. last is written back into the last cell.

125920701234i

i < n − 1 is false (4 < 4), but last == target is true → 4 is returned. Without this second condition, the real last element would count as “not found”. If 4 were the target, the same loop would stop at i = 4 and return −1.

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;
}

Output Question · Binary Search

Question 3: Printed Middle Elements

For the array [2, 3, 4, 7, 8, 9, 10, 21, 22, 31] and target 40, what does the code below print, and what does it return?

int lo = 0;
int hi = arr.length - 1;
while (lo <= hi) {
  int mid = lo + (hi - lo) / 2;
  System.out.println(arr[mid]);
  if (arr[mid] == target) {
    return mid;
  } else if (arr[mid] > target) {
    hi = mid - 1;
  } else {
    lo = mid + 1;
  }
}
return -1;
Show answer

Output: 8 21 22 31, returns −1.

lohimidarr[mid]
0948
59721
89822
99931

Since 40 is larger than every element, each round goes right; the loop ends when lo = 10 > hi = 9. 4 rounds = ⌊log₂ 10⌋ + 1 (the worst case).

Multiple Choice · Binary Search

Question 4: Two Million Elements

In a sorted array of 2,000,000 elements, how many rounds (middle comparisons) does binary search make in the worst case?

C: At most ⌊log₂ n⌋ + 1 rounds: 2²⁰ = 1,048,576 ≤ 2,000,000 < 2²¹ → ⌊log₂ n⌋ = 20 → 21 rounds. 20 comes from forgetting the “+ 1” in ⌊log₂ n⌋; 1,000,000 is the average of linear search.

Multiple Choice · Binary Search

Question 5: Forgetting the ±1

In binary search, if hi = mid - 1; is replaced by hi = mid; (loop condition lo <= hi), what can happen?

C: With lo = hi = mid, if the target is smaller than arr[mid], hi = mid and the range never changes; the loop runs forever. The middle element has already been checked, so the bounds must be updated by ±1.

Classic Question · Binary Search

Question 6: The Implicit Search Tree

Draw the comparison tree of binary search on the array [4, 9, 13, 20, 26, 31, 40]. Which elements are found with 1, 2 and 3 comparisons? How many comparisons does a successful search make on average?

Show answer

The root is the first middle element (20); the middle of the left half is 9, that of the right half 31; the rest are leaves:

209314132640
  • 1 comparison: 20 · 2 comparisons: 9, 31 · 3 comparisons: 4, 13, 26, 40

Average (1 + 2·2 + 4·3)/7 = 17/7 = 2.43. The tree has ⌊log₂ 7⌋ + 1 = 3 levels: the worst case. The tree is never built in memory; the middle computation traverses it implicitly.

Multiple Choice · Binary Search

Question 7: Finding the First Position

To find the first position of the target value in a sorted array with duplicates, what should be done in the equality case (arr[mid] == target)?

D: The match is recorded and the left half is searched for an earlier match; result is returned when the loop ends. The time stays O(log n). C also gives the correct result, but degrades to O(n) when all elements are equal; A returns an arbitrary match, B goes to the last position.

Output Question · Hash Search

Question 8: Character-Sum Hash

Which buckets does the hash function from the slides give for the keys "kar", "ark" and "kara" with m = 10? (Unicode: a = 97, k = 107, r = 114)

int hash(String key, int m) {
  int sum = 0;
  for (char k : key.toCharArray())
    sum += k;
  return sum % m;
}
Show answer
  • "kar": 107 + 97 + 114 = 318 → 8
  • "ark": 97 + 114 + 107 = 318 → 8 (collision)
  • "kara": 318 + 97 = 415 → 5

The sum ignores the order of letters: words made of the same letters always collide. Java's String.hashCode() reduces this problem by using position-dependent weights (Σ s[i] · 31ⁿ⁻¹⁻ⁱ).

Step-by-Step Trace · Hash Search

Question 9: Insertion with Linear Probing

The keys 23, 43, 13, 27, 37 are inserted, in this order, into an empty table (m = 10) with hashInsert. Which bucket does each key land in? Then, if 33 is searched for, how many buckets are probed?

0123456789

Empty table, m = 10, h(x) = x % 10. The keys 23, 43, 13, 27, 37 will be inserted in order; on a full bucket we move to the next one (linear probing).

230123456789

h(23) = 3: the bucket is empty → 23 is placed here.

23430123456789

h(43) = 3 is full → 4 is probed; the first empty bucket is 4. (2 probes)

2343130123456789

h(13) = 3 is full → 4, 5 are probed; the first empty bucket is 5. (3 probes)

234313270123456789

h(27) = 7: the bucket is empty → 27 is placed here.

23431327370123456789

h(37) = 7 is full → 8 is probed; the first empty bucket is 8. (2 probes)

23431327370123456789

hashSearch(33): h = 3 → compared with 23, 43, 13, bucket 6 is empty → −1. 4 buckets were probed: buckets 3, 4, 5 form a contiguous cluster (primary clustering).

void hashInsert(Integer[] table,
              int key) {
  int m = table.length;     // m > 0
  int idx = Math.floorMod(key, m);
  while (table[idx] != null)
    idx = (idx + 1) % m; // next
  table[idx] = key;
}

Multiple Choice · Hash Search

Question 10: A Full Table

If a key that is not in the table is searched for with the linear-probing hashSearch method in a completely full table, what happens?

B: The loop stops only at an empty (null) bucket; since % m wraps around from the end to the start, it does not leave the array either, and it cycles through the table forever. Fix: limit the load factor (e.g. grow the table once α exceeds 0.75) or stop after m probes.

Step-by-Step Trace · Interpolation Search

Question 11: Interpolation on Skewed Data

interpolationSearch(arr, 8, 32) runs on the array [2, 4, 8, 16, 32, 64, 128, 256]. Which index is estimated in each round, and how many probes are made?

24816326412825601234567

The array is sorted but grows exponentially (2, 4, 8, …, 256): the uniform-distribution assumption is violated. Target: 32.

24816326412825601234567idx

idx = 0 + 210 / 254 = 0 (rounded down); arr[0] = 2 < 32 → lo = 1. The value 256 pulls the estimate to the left.

24816326412825601234567idx

idx = 1 + 168 / 252 = 1 (rounded down); arr[1] = 4 < 32 → lo = 2.

24816326412825601234567idx

idx = 2 + 120 / 248 = 2 (rounded down); arr[2] = 8 < 32 → lo = 3.

24816326412825601234567idx

idx = 3 + 64 / 240 = 3 (rounded down); arr[3] = 16 < 32 → lo = 4.

24816326412825601234567idx

idx = 4 + 0 / 224 = 4; arr[4] = 32 → return 4. 5 probes: binary search would find it in 3 probes (3, 5, 4).

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;
    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;
}

Multiple Choice · Interpolation Search

Question 12: Overflow in the Intermediate Product

In interpolation search, which statement about the product (target - arr[lo]) * (hi - lo) is correct?

D: For example, if the values are on the order of 10⁹ and hi − lo is 10⁶, the product far exceeds the int limit (2³¹ − 1). It must be computed with long. Dividing first (C) changes the result in integer division: it often comes out as 0.

Step-by-Step Trace · Jump Search

Question 13: Jump Search: Unsuccessful

jumpSearch(arr, 30, 16) runs on D = [3, 8, 15, 21, 27, 34, 40, 46, 52, 59, 65, 71, 78, 84, 90, 97]. Which indices are probed, and what is returned?

3815212734404652596571788490970123456789101112131415

n = 16 → block = √16 = 4. The target is 30; the last element of each block will be examined.

3815212734404652596571788490970123456789101112131415probe

arr[min(4, 16) − 1] = arr[3] = 21 < 30 → skip the block.

3815212734404652596571788490970123456789101112131415probe

pos = 4, jump = 8. arr[7] = 46 ≥ 30 → the jumping ends; the target can be in the block [4, 8).

3815212734404652596571788490970123456789101112131415probe

Linear scan: arr[4] = 27 < 30 → pos = 5.

3815212734404652596571788490970123456789101112131415probe

pos = 5 ≠ min(8, 16); arr[5] = 34 < 30 is false → we leave the loop.

3815212734404652596571788490970123456789101112131415

arr[5] = 34 ≠ 30 → −1. The probed indices are 3, 7, 4, 5: 30 would lie between 27 and 34. The worst case is ≈ n/m + m − 1 = 7 probes.

int jumpSearch(int[] arr, int target,
    int n) { 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;
    if (pos >= n) return -1;
  }
  while (arr[pos] < target) {
    pos++;
    if (pos == Math.min(jump, n)) return -1;
  }
  if (arr[pos] == target) return pos;
  return -1;
}

Multiple Choice · Jump Search

Question 14: The Best Block Size

In jump search with block size m, the worst case is ≈ n/m + (m − 1) probes. For n = 10,000, what are the best m and the worst-case number of probes?

A: f(m) = n/m + m − 1, f′(m) = −n/m² + 1 = 0 ⇒ m = √n = 100; 10,000/100 + 99 = 199 → O(√n). For m = 50 it is 200 + 49 = 249 probes; 14 is the number of probes of binary search (⌊log₂ 10,000⌋ + 1).

Output Question · Exponential Search

Question 15: Probes in Exponential Search

exponentialSearch(D, 27, 16) call, with D = [3, 8, 15, 21, 27, 34, 40, 46, 52, 59, 65, 71, 78, 84, 90, 97]: which indices are probed and what is returned? On which range does binary search run?

int expSearch(int[] a, int target, int n) {
  if (n == 0) return -1;
  if (a[0] == target) return 0;
  int bound = 1;
  while (bound < n && a[bound] <= target)
    bound *= 2;
  int lo = bound / 2;
  int hi = Math.min(bound, n - 1);
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (a[mid] == target) return mid;
    if (a[mid] < target) lo = mid + 1;
    else hi = mid - 1;
  }
  return -1;
}
Show answer
  • a[0] = 3 ≠ 27
  • Doubling: a[1] = 8, a[2] = 15, a[4] = 27 ≤ 27 → bound 1 → 2 → 4 → 8; a[8] = 52 > 27 → stops.
  • The range is [8/2, min(8, 15)] = [4, 8]; binary search: mid 6 (40 > 27) → hi = 5; mid 4 → 27 found.

Probed indices: 0, 1, 2, 4, 8, 6, 4 (7 probes); returns 4. Index 4 is probed twice: the ≤ test in the doubling phase and the equality test in the binary search. The cost is O(log i), with i = 4.

Multiple Choice · Exponential Search

Question 16: When Is Exponential Search Best?

When is exponential search more suitable than binary search?

C: The cost depends not on n but on the index i of the target: O(log i). In lists of unknown (unbounded) size, binary search first needs an upper bound; doubling finds one. A, B and D are the cases where linear, interpolation and jump search, respectively, are suitable.

Multiple Choice · Comparison

Question 17: Which Search?

Sorted records are kept on a magnetic tape; reading forward is cheap, rewinding is very expensive. Which search method is the most suitable?

B: Jump search always moves forward in blocks of √n; it goes back only once, to the last block, and reads it sequentially: O(√n). Binary and interpolation search make large jumps back and forth; a hash table requires the records to be placed into a separate table.

Classic Question · Comparison

Question 18: Linear or Binary?

Compare linear and binary search by their advantages and disadvantages. If k queries will be made on an unsorted array of n elements, which one would you choose, and under what condition?

Show answer
  • Linear: no precondition, simple, also works on a linked list; O(n) in the worst and average case.
  • Binary: O(log n), very fast on large data; but it needs a sorted array and O(1) access by index.

k queries: linear total O(k · n); sort once + binary O(n log n + k log n). If k is small (e.g. a single query), linear; once k exceeds the order of log n, the cost of sorting pays for itself. Example: n = 10⁶, k = 1000: linear ≈ 10⁹, sort + binary ≈ 2 · 10⁷ + 2 · 10⁴ comparisons.

If only exact matches are needed, a hash table is a third option with O(1) on average.

The End

Chapter 4: Searching Algorithms: Study Questions
Lecture slides: Chapter 4: Searching Algorithms · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.