Binary Search
Binary search works with sorted item in collection. The main idea of binary search is started by dividing an array in the middle index, left and right. It checks whether the target input smaller or bigger than value of middle array.
If smaller, the array will be divided again into left and right side based on first index until middle index. It keeps searching (looping) until target value found.
Watch the patterns:
- If we have 4 items in array, it performs max 2 times of searching.
- If we have 8 items in array, it performs max 3 times of searching.
- If we have 10 items in array, it performs between 3 - 4 times of searching.
- and so on.
Therefore, the complexity of Binary Search is O(log N).