Binary search¶
Content summary
This lesson introduces the binary search algorithm.
Overview¶
Review the general introduction to the search problem and search algorithms here.
Binary search algorithm¶
Idea¶
Imagine you’re looking for a word that starts with the letter T in a dictionary.
The dictionary is already open to some page.
You don’t start from page 1. Instead, you look at the current page and decide: "Is the letter T before or after this page?". Then you continue searching only in the half that can contain T.
This is the key idea: keep cutting the search area in half.
Applied to an array, binary search works as follows:
- Determine the middle element to split the original array into two sub-arrays: the left half and the right half.
- Compare the middle element with the target value
kin order to eliminate the half that cannot containk. - Repeat the above two steps on the remaining sub-array until either
kis found or the array can no longer be divided in half.
Here’s how it works in detail:
-
Set two pointers:
left= index of the first elementright= index of the last element
-
While
leftis still less than or equal toright, repeat the following steps:- Calculate the middle index:
mid = (left + right) // 2(integer division) -
Compare the middle element with the target
k:- If
A[mid] == kthen found! Returnmidand stop. - If
A[mid] < kthen the target must be in the right half. Discard the left half:left = mid + 1 - If
A[mid] > kthen the target must be in the left half. Discard the right half:right = mid - 1
- If
- Calculate the middle index:
-
When the loop ends (
left > right), we have no more elements to check.kis not in the array. Return-1.(
-1is the standard convention for “not found”, since valid array indices start at 0.)
Example illustration¶
Flowchart¶
Visualization¶
Writing the program¶
1. Import the numpy library.
2. Write the function binary_search() to implement the binary search algorithm.
The function takes two input parameters:
- The array
A - The target value
k
It returns:
- The index where
kis found, or -1ifkis not found.
3. Write the main program.
For simplicity, we will not ask the user to input the entire array. Instead, we create a predefined sorted array in the code, and the user only needs to enter the value they want to search for.
Important note
Before using binary search, the array must be sorted (in ascending or descending order).
We use the sort() function from numpy to sort the array Array in ascending order (line 35). The sorted array is stored in sorted_Array.
Call the binary_search() function and store the result (the index where the value was found, or -1) in the variable found (line 41).
Based on the value of found, print an appropriate message:
- If the value was found (
found ≠ -1), display its index. - If the value was not found (
found == -1), display a "not found" message.
4. Run the program above and enter 4. The output will be:
Run the program again and enter 6 as the value to search for. The output will be:
Comparison of the two search algorithms¶
Here are the main differences between the two algorithms:
| Feature | Sequential search | Binary search |
|---|---|---|
| Idea | Check each element one by one from the start until found. | Repeatedly check the middle element and decide which half to continue searching. |
| Position returned | The first occurrence from the beginning of the array. | Any occurrence (depends on the middle element checked). |
| Best suited for | Small datasets or unsorted data. | Large datasets that are already sorted. |
| Time complexity | \(O(n)\) | \(O(\log n)\) |
Source code¶
The complete code is available at:
Summary mindmap¶
Some English words¶
| Vietnamese | Tiếng Anh |
|---|---|
| tìm kiếm nhị phân | binary search |
| mảng con | subarray |