Binary Search Algorithm
Binary search is an efficient search algorithm that finds the location of a search key within a sorted array. Binary search compares the search key to the key of the middle element of the array. If they are not equal, the half in which the search key cannot be located is eliminated and the search continues on the remaining half, again taking the middle element's key to compare to the search key, and repeating this until the search key is found. If the search ends with the remaining half being empty, the search key is not in the array.
Pseudocode
procedure binary_search(array : sorted list of items to search, n : length of list, search_key : key to search for)
low ← 0
high ← n - 1
while low <= high
mid ← (low + high) / 2
if search_key == array[mid]
return mid
end if
if search_key < array[mid]
high ← mid - 1
else
low ← mid + 1
end if
end while
return -1
end procedure
Examples
Example 1: A successful search
In this first example, the binary_search() function is called to search for the key 48.
low ← 0
high ← n - 1 // n is 12, so high is 11
low <= high is true, loop entered
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
mid ← (low + high) / 2 // (0 + 11) / 2 = 5
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (48 is not equal to 51)
search_key < array[mid] is true (48 is less than 51)
high ← mid - 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is true, loop continues
mid ← (low + high) / 2 // (0 + 4) / 2 = 2
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (48 is not equal to 37)
search_key < array[mid] is false (48 is not less than 37)
low ← mid + 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is true, loop continues
mid ← (low + high) / 2 // (3 + 4) / 2 = 3
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (48 is not equal to 42)
search_key < array[mid] is false (48 is not less than 42)
low ← mid + 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is true, loop continues
mid ← (low + high) / 2 // (4 + 4) / 2 = 4
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is true (48 is equal to 48)
return mid // Function returns 4
Example 2: A failed search
In this second example, the binary_search() function is called to search for the key 62.
low ← 0
high ← n - 1 // n is 12, so high is 11
low <= high is true, loop entered
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
mid ← (low + high) / 2 // (0 + 11) / 2 = 5
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (62 is not equal to 51)
search_key < array[mid] is false (62 is not less than 51)
low ← mid + 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is true, loop continues
mid ← (low + high) / 2 // (6 + 11) / 2 = 8
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (62 is not equal to 64)
search_key < array[mid] is true (62 is less than 64)
high ← mid - 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is true, loop continues
mid ← (low + high) / 2 // (6 + 7) / 2 = 6
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (62 is not equal to 55)
search_key < array[mid] is false (62 is not less than 55)
low ← mid + 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is true, loop continues
mid ← (low + high) / 2 // (7 + 7) / 2 = 7
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
search_key == array[mid] is false (62 is not equal to 58)
search_key < array[mid] is false (62 is not less than 58)
low ← mid + 1
|
|
array
|
12
|
23
|
37
|
42
|
48
|
51
|
55
|
58
|
64
|
68
|
75
|
79
|
|
|
low <= high is false, loop ends
return -1
Complexity
Time Complexity: O(log n)