Searching finds the position of a given value, called the key, in a collection. The best method depends on whether the data is sorted.
Linear Search
Linear search, or sequential search, compares the key with each element in turn, from the first, until it finds a match or reaches the end. It works on any array, sorted or not. In the best case the key is the first element, taking one comparison. In the worst case, the key is last or absent, taking n comparisons. So linear search is O(n). It is simple and suitable for small or unsorted data.
Binary Search
Binary search works only on a sorted array. It compares the key with the middle element. If they are equal, the search ends. If the key is smaller, the search continues in the left half, otherwise in the right half. Each comparison halves the part that remains, so at most about log₂ n + 1 comparisons are needed. Binary search is O(log n). For a million elements, it needs about 20 comparisons, against a million for linear search. The steps use three indices: low, high and mid = (low + high)/2.
Definition: Binary search. A search method for a sorted array that repeatedly compares the key with the middle element and discards the half in which the key cannot lie.
Comparison of Methods
Linear search needs no sorting and works on linked lists, but is slow for large data. Binary search is very fast, but needs sorted data and direct access to the middle element, so it suits arrays and not linked lists. If data is searched many times, it is worth sorting it once and then using binary search, which is another case of the time-space tradeoff.
Table 1.4: Linear and binary search
| Feature | Linear search | Binary search |
|---|---|---|
| Data required | Any order | Sorted |
| Worst case | O(n) | O(log n) |
| Best case | O(1) | O(1) |
| Suitable for | Small or unsorted data, lists | Large sorted arrays |
Example 1.4: Binary search trace
Problem: Search for 37 in the sorted array {5, 12, 19, 23, 37, 45, 52, 60} using binary search.
Solution:
- low = 0, high = 7, mid = 3. a[3] = 23 < 37, so low = 4.
- low = 4, high = 7, mid = 5. a[5] = 45 > 37, so high = 4.
- low = 4, high = 4, mid = 4. a[4] = 37. Found.
Result: 37 is found at index 4 after 3 comparisons.
#include <stdio.h>
int binary(int a[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
printf("low=%d high=%d mid=%d\n", low, high, mid);
if (a[mid] == key) return mid;
if (a[mid] < key) low = mid + 1; else high = mid - 1;
}
return -1;
}
int main(void) {
int a[] = {5, 12, 19, 23, 37, 45, 52, 60};
printf("found at %d\n", binary(a, 8, 37));
return 0;
}
Output
low=0 high=7 mid=3
low=4 high=7 mid=5
low=4 high=4 mid=4
found at 4
Common mistakes
- Using binary search on unsorted data.
- Writing low < high instead of low <= high, which misses the last element.
- Not moving low to mid + 1 or high to mid − 1, which can loop forever.
Key takeaways
- Linear search works on any data and is O(n).
- Binary search needs sorted data and is O(log n).
- Binary search uses low, high and mid.
- For many searches, sort once and use binary search.
Practice questions
- What condition must hold for binary search?
- What is the worst-case complexity of linear search?
- How many comparisons does binary search need for about 1000 elements?
Answers to the practice questions
- The array must be sorted.
- O(n).
- About 10.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit I
