Binary Search
Binary search
binary search: Locates a target value in a sorted
array/list by successively eliminating half of the array from
consideration.
How many elements will it need to examine? O(log N)
Can be implemented with a loop or recursively
Example: Searching the array below for the value 42:
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103
min mid max
2
Binary search code
// Returns the index of an occurrence of target in a,
// or a negative number if the target is not found.
// Precondition: elements of a are in sorted order
public static int binarySearch(int[] a, int target) {
int min = 0;
int max = a.length - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (a[mid] < target) {
min = mid + 1;
} else if (a[mid] > target) {
max = mid - 1;
} else {
return mid; // target found
}
}
return -(min + 1); // target not found
}
3
Recursive binary search
Write a recursive binarySearch method.
If the target value is not found, return its negative insertion
point.
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103
int index = binarySearch(data, 42); // 10
int index2 = binarySearch(data, 66); // -14
4
Exercise solution
// Returns the index of an occurrence of the given value in
// the given array, or a negative number if not found.
// Precondition: elements of a are in sorted order
public static int binarySearch(int[] a, int target) {
return binarySearch(a, target, 0, a.length - 1);
}
// Recursive helper to implement search behavior.
private static int binarySearch(int[] a, int target,
int min, int max) {
if (min > max) {
return -1; // target not found
} else {
int mid = (min + max) / 2;
if (a[mid] < target) { // too small; go right
return binarySearch(a, target, mid + 1, max);
} else if (a[mid] > target) { // too large; go left
return binarySearch(a, target, min, mid - 1);
} else {
return mid; // target found; a[mid] == target
}
}
}
5
Binary Search
Example: sorted array of integer keys.
Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
6
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Find approximate midpoint
7
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Is 7 = midpoint key? NO.
8
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Is 7 < midpoint key? YES.
9
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Search for the target in the area before midpoint.
10
Binary Search
Example: sorted array of integer keys.
Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Find approximate midpoint
11
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Target = key of midpoint? NO.
12
Binary Search
Example: sorted array of integer keys.
Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Target < key of midpoint? NO.
13
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Target > key of midpoint? YES.
14
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Search for the target in the area after midpoint.
15
Binary Search
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Find approximate midpoint.
Is target = midpoint key? YES.
16
Relation to Binary Search Tree
Array of previous example:
3 6 7 11 32 33 53
orresponding complete binary search tree
11
6 33
3 7 32 53
17
Search for target = 7
Find midpoint:
3 6 7 11 32 33 53
Start at root:
11
6 33
3 7 32 53
18
Search for target = 7
Search left subarray:
3 6 7 11 32 33 53
Search left subtree:
11
6 33
3 7 32 53
19
Search for target = 7
Find approximate midpoint of
subarray:
3 6 7 11 32 33 53
Visit root of subtree:
11
6 33
3 7 32 53
20
Search for target = 7
Search right subarray:
3 6 7 11 32 33 53
Search right subtree:
11
6 33
3 7 32 53
21
Binary Search: Analysis
Best Case: O(1)
Worst case complexity?
What is the maximum depth of recursive calls in binary
search as function of n?
Each level in the recursion, we split the array in half (divide
by two).
Therefore maximum recursion depth is floor(log2n) and
worst case = O(log2n).
Average case is also = O(log2n).
Worst case of Linear search: O(n)
Best case of Linear search: O(1)
22