|
| 1 | +# Binary Search |
| 2 | +Binary search is a search algorithm that finds the position of a target value within a sorted array or list. It compares |
| 3 | +the target value to the middle element of the search range, then, based on the comparison, narrows the search to the |
| 4 | +upper or lower half of the current search range. |
| 5 | + |
| 6 | +Two versions of binary search has been implemented in this repository - BinarySearch and BinarySearchTemplated. |
| 7 | + |
| 8 | +## BinarySearch |
| 9 | + |
| 10 | +Image Source: GeeksforGeeks |
| 11 | + |
| 12 | +BinarySearch is a more straightforward and intuitive version of the binary search algorithm. In this approach, after the |
| 13 | +mid-value is calculated, the high and low pointers are adjusted by just one unit. From the above example, after mid |
| 14 | +points to index 4 in the first search, the low pointer moves to index 5 (+1) when narrowing the search. Similarly, when |
| 15 | +mid points to index 7 in the second search, the high pointer shifts to index 6 (-1) when narrowing the search. This |
| 16 | +prevents any possibility of infinite loops. |
| 17 | + |
| 18 | +## BinarySearchTemplated |
| 19 | + |
| 20 | +BinarySearchTemplated removes the condition that checks if the current mid-value is equal to the target (which helps to |
| 21 | +end the search the moment the target is found). The template adds a "condition" method which will be modified based on |
| 22 | +the requirements of the implementation. |
| 23 | + |
| 24 | +The narrowing of the search space differs from BinarySearch - only one of the high or low pointers will be adjusted by |
| 25 | +one unit. |
| 26 | + |
| 27 | +This template will work for most binary search problems and will only require the following changes: |
| 28 | +- Search space (high and low) |
| 29 | +- Condition method |
| 30 | +- Returned value (low or low - 1) |
| 31 | + |
| 32 | +### Search Space (Requires change) |
| 33 | +Simply modify the initialisation of the high and low pointer according to the search space. |
| 34 | + |
| 35 | +### Condition (Requires change) |
| 36 | +We assume that when the condition returns true, the current value "passes" and when the condition returns false, the |
| 37 | +current value "fails". |
| 38 | + |
| 39 | +In this template, we want to find the first "pass" in the array. |
| 40 | + |
| 41 | +INSERT IMAGE OF FIRST PASS |
| 42 | + |
| 43 | +### Returned Value (Requires change) |
| 44 | +In the implementation of BinarySearchTemplated, return low was used to find the first "pass". |
| 45 | + |
| 46 | +EXPLANATION TBC, STILL THINKING HOW TO PHRASE IT. |
| 47 | + |
| 48 | +### Search Space Adjustment |
| 49 | +What should be the search space adjustment? (Why only low = mid + 1) |
| 50 | + |
| 51 | +Due to the nature of floor division in Java's \ operator, the searched mid-index will always be smaller than the high |
| 52 | +pointer of the previous search range. On the other hand, low = mid + 1, ensures that the searched mid-index is always |
| 53 | +larger than the low pointer of the previous search range. This ensures that the search range is narrowed in every loop |
| 54 | +and prevents the possibility of infinite loops. |
| 55 | + |
| 56 | +INSERT IMAGE HERE TO EXPLAIN |
| 57 | + |
| 58 | +As we close in towards the target value, the final low = mid + 1 narrows the search range from low. TBC ON EXPLANATION |
| 59 | + |
| 60 | +Credits: [Powerful Ultimate Binary Search Template](https://leetcode.com/discuss/general-discussion/786126/python-powerful-ultimate-binary-search-template-solved-many-problems) |
| 61 | + |
| 62 | +## Complexity Analysis |
| 63 | +**Time**: |
| 64 | +- Worst case: O(log n) |
| 65 | +- Average case: O(log n) |
| 66 | +- Best case: |
| 67 | + - BinarySearch O(1) |
| 68 | + - BinarySearchTemplated O(log n) |
| 69 | + |
| 70 | +BinarySearch: |
| 71 | +In the worst case, the target is either in the first index or does not exist in the array at all. |
| 72 | +In the best case, the target is the middle (odd number of element) or the first middle element (even number of elements) |
| 73 | +if floor division is used to determine the middle. |
| 74 | + |
| 75 | +BinaryTemplated: |
| 76 | +In all cases, O(log n) iterations will be required as there is no condition to exit the loop prematurely. |
| 77 | + |
| 78 | +**Space**: O(1) since no new data structures are used and searching is only done within the array given |
0 commit comments