Search algorithms: linear and binary
Linear search works on any list, while binary search finds an element very quickly in a sorted array. You will learn both methods and when to choose each.
All topics (2 of 11)
Chapter 1. Algorithms
Chapter 2. Data structures
Chapter 3. Databases
Chapter 4. Modern technologies and projects
Searching means finding a required element in a collection or finding out that it is not there. The simplest method is linear search: elements are checked one by one from the start, and the search stops when the element is found. It works on any list, but in the worst case all n elements must be examined. In Python, for a list, x in a and a.index(x) work exactly like this.
Binary search works only on a sorted array. It compares the target with the middle element: if the target is smaller, the right half is discarded, and if it is larger, the left half. The process repeats on the remaining part, so at each step the search area shrinks by about half. The middle index is found with mid = (low + high) // 2.
Let us search for 23 in the sorted array 2 5 8 12 16 23 38 56 72 91. In step 1 the middle element 16 is smaller than the target, so the search continues in the right half (23 38 56 72 91). In step 2 the middle element 56 is larger, so the left part (23 38) remains; in step 3 the middle element equals 23 and the number is found.
In linear search the number of comparisons is proportional to the number of elements, while binary search needs at most about log2 n. For example, in a sorted array of 1,000,000 elements binary search takes at most 20 steps, while linear search makes up to a million comparisons. However, if the array first has to be sorted, linear search is simpler for a single lookup; for many lookups it pays to sort once and then use binary search.
Key takeaways
- Linear search checks elements one by one and works on any list.
- Binary search works only on a sorted array.
- Binary search cuts the search area roughly in half at every step.
- For 1,000,000 elements binary search takes at most 20 steps, while linear search makes up to a million comparisons.
- Before many searches, it pays to sort the array once.
Practice tasks
- Find 24 by hand in the sorted array
3 7 11 15 19 24 31 40using binary search: at every step write the values oflow,highandmidin a table. - Write a binary search function in Python: it should return the index of the element, or
-1if the element is not found. - Also write a linear search function, add a comparison counter to both functions and compare the numbers of comparisons on a sorted list of 1000 numbers.
Review questions
Test yourself: think first, then reveal the answer.
1Why can binary search not be used on an unsorted array?Show answerHide answer
Discarding a half is correct only if the array is ordered; otherwise the target may be in the discarded half.2How does the search area change after each step of binary search?Show answerHide answer
It becomes about half as large.3Why is linear search often more convenient for a single lookup?Show answerHide answer
Sorting the list takes longer than a linear search, so for one lookup sorting is an unnecessary cost.
0 of 11 topics completed
Sign in to mark