date posted: 2020-01-11
Binary search is very straight forward, it only works when list is sorted.
I will quickly explain its working steps with an example:
So main goal of binary search is finding the midpoint.
there are few different ways of choosing a midpoint
so using the first method in our case of 10-element list. first midpoint would be 0 + 9/2 = 4.5, rounded down to 4. and 4 means index, 4th element which is 16.
import math def binarySearch(lst, key): low = 0 end = len(lst) -1 while low <= end: mid = math.floor((low + end)/2) if key == lst[mid]: return f'key {key} = lst[{mid}]' elif key < lst[mid]: end = mid - 1 else: low = mid + 1 return f'{key} is not in this list'
When given ordered list and perform binary search
but what if list is not ordered?
In such case we would need to add time complexity of sorting algorithm + time complexity of binary search
Depending on the use case you would need to use linear search or binary search.
ex: if you have unsorted list with million elements and you are only going to search once in this case you would just use linear search. Using linear search will give O(n) = n, however using sorting then binary search would give O(n log n) + O(log n) = O(n log n).
BUT if you need to keep on using search algorithm on that list, it would be much better
to sort once, then keep on using binary search since as search frequency increase
using binary search will make up for its sorting algorithm in the beginning.
1. for sorting a list once then keep on applying binary search:
2. for linear search:
let number_of_search = 100
len(list) = 10
note: log10 = 3.321928... (log base 2)
so using 1st method, 10 log(10) + 100(log(10)) = 365.41208
using 2nd methos, 100(10) = 1000.
We can clearly see even for 100 search sorting a list once then applying binary search would be much more efficient way. So if you are going to search multiple times, you would use method 1. If only few times, use method 2.