Searching Algorithms
Lesson 5 of 20Chase 49 through a sorted array as low, mid and high halve the window — then watch linear search crawl the long way.
Two ways to hunt
Finding one value inside a million is the most common job in computing — the trick is to stop looking at values you can already rule out.
Two contestants
Linear search checks elements one by one, in any order. Binary search only works on sorted data — but it is dramatically faster.
The sorted shortcut
If a[mid] is smaller than the target, everything left of mid is smaller too. One comparison eliminates half the array.
Halving power
1,024 elements take at most 10 comparisons; 1,000,000 elements take about 20. Every probe halves the window.
Java ships one
Arrays.binarySearch(a, 49) does it for you — it returns the index, or -(insertionPoint) - 1 when the value is missing.
Where you meet it
Dictionary pages, git bisect, database indexes and autocomplete all ride on binary search or its cousins.
Binary in motion
Ten sorted scores, one target. Watch low, mid and high corner 49 in four comparisons.
A sorted array of 10 scores — we are hunting for 49. The window starts as the whole array: low = 0, high = 9.
The flow
One loop, one compare, one halve — the whole algorithm fits in a handful of boxes.
Walkthrough
The same hunt in real Java — binary search on an int[] first, then the linear scan for comparison.
1public class BinarySearch {2public static void main(String[] args) {3int[] a = {5, 11, 18, 26, 34, 41, 49, 57};4int target = 49;5int low = 0, high = a.length - 1;67while (low <= high) {8int mid = (low + high) / 2;9if (a[mid] == target) {10System.out.println("found at " + mid);11return;12}13if (a[mid] < target) low = mid + 1;14else high = mid - 1;15}16System.out.println("not found");17}18}
a gets 8 sorted scores — binary search demands sorted input.
Linear in motion
Same target, no sorted order — linear search must peek into every box until it stumbles on 49.
An unsorted array — binary search is off the table. Linear search starts at index 0 and walks right.
Predict the output
Trace low, mid and high by hand before you guess — integer division matters.
1public class Predict {2public static void main(String[] args) {3int[] a = {6, 13, 20, 27, 34, 41, 48, 55, 62};4int target = 55;5int low = 0, high = 8;6while (low <= high) {7int mid = (low + high) / 2;8if (a[mid] == target) {9System.out.println("hit " + mid);10return;11}12if (a[mid] < target) low = mid + 1;13else high = mid - 1;14}15System.out.println("miss");16}17}
Hint: mid = (low + high) / 2 always floors. Watch how the window closes in on 55.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Binary search demands a sorted array — that is the price of O(log n) speed.
Every comparison halves the window: 1,000,000 elements take about 20 checks.
mid = (low + high) / 2 uses integer division — (0 + 7) / 2 is 3, not 3.5.
low > high means the window is empty: return -1.
Linear search works on any data but pays up to n comparisons for it.
Arrays.binarySearch is the JDK version — its negative miss value even encodes where the target would go.