Read · 1 min

Linear Search

The linear search is just going from the first element to a last element of your data, stopping if you find the element you're looking for. In the worst case, if you have nn terms to search through, so this algorithm runs in O(n)O(n).

Binary Search

Binary search is a great search that works when your data is already in order. Think of it as that game where Player 1 thinks of a number, and Player 2 must try to guess that number after Player 1 says lower or higher. Well binary search does just that.

Since the data is already in order, we will know when we have passed an element. So if we have nn elements, look at element n/2n/2. If it is too high, then perform the algorithm in the interval [1,n/2)[1,n/2). If it is too low, then perform the algorithm in the interval (n/2,n](n/2,n].

Say we wanted to find the index of the element 2020 in the array A=[1,2,4,5,8,10,16,20,24,30,32,33]A=[1,2,4,5,8,10,16,20,24,30,32,33]. Since there are 1212 elements, look at the 66th element, 1010. This is too low, so look in the interval [7,12][7,12]. Check the middle term, the 99th term, which is 2424. This is too high again, so check in the interval [7,8][7,8]. Check the middle term, the 77th term. This is too low again, so check the 88th term and we win.

The binary search algorithm runs in O(log⁡n)O(\log n) since if you have n=2kn=2^k elements, it will need to halve k=log⁡2nk=\log_2n times to terminate. (By the way: O(log⁡n)O(\log n) is really good! It certainly pays off to keep your data sorted if you plan on searching in it a lot.)

Type a concept, like “slope” or “photosynthesis”.

↑ ↓ to choose · Enter to openSearch explanations and problems