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.
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
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
high
|
mid ← (low + high) / 2 // (0 + 11) / 2 = 5
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
mid
|
high
|
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
high
|
mid
|
high |
low <= high is true, loop continues mid ← (low + high) / 2 // (0 + 4) / 2 = 2
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
mid
|
high
|
mid |
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low |
mid
|
low
|
high
|
low <= high is true, loop continues mid ← (low + high) / 2 // (3 + 4) / 2 = 3
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
mid |
low mid
|
high
|
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low mid
|
low
high
|
low <= high is true, loop continues mid ← (low + high) / 2 // (4 + 4) / 2 = 4
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
mid |
low
high
mid
|
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
high
|
mid ← (low + high) / 2 // (0 + 11) / 2 = 5
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
mid
|
high
|
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low |
mid
|
low
|
high
|
low <= high is true, loop continues mid ← (low + high) / 2 // (6 + 11) / 2 = 8
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
mid |
low
|
mid
|
high
|
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low
|
high
|
mid
|
high |
low <= high is true, loop continues mid ← (low + high) / 2 // (6 + 7) / 2 = 6
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low mid
|
high
|
mid |
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
low mid
|
low high
|
low <= high is true, loop continues mid ← (low + high) / 2 // (7 + 7) / 2 = 7
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
mid |
low high mid
|
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
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
12 | 23 | 37 | 42 | 48 | 51 | 55 | 58 | 64 | 68 | 75 | 79 |
high mid
|
low
|
low <= high is false, loop ends return -1
Time Complexity: O(log n)