| Learn | Algorithm - Searching - Binary search on sorted numbers |
|---|
| Binary Search: On Sorted Numbers It's easiest to show a binary search on numbers using pseudo-code Do not attempt to return early by comparing array[mid] to x for equality. The extra comparison can only slow the code down. Note you need to add one to low to avoid becoming trapped by integer division always rounding down. Interestingly, the above version of binary search allows you to find the smallest occurrence of x in the array. If the array contains duplicates of x, the algorithm can be modified slightly in order for it to return the largest occurrence of x by simply adding to the if conditional: Note that instead of doing |
Read Article
https://develop.horje.com/learn/1434/reference