Bi-partition
definition
The binary search method is an algorithm whose input is an ordered list of elements.
If the element being searched is contained in the list, binary search returns its position; otherwise it returns null.
Basic idea
- This method is suitable when the amount of data is large.
- When using binary search, the data must be sorted
- Assuming the data is sorted in ascending order, for a given value key, start comparing from the middle position mid of the sequence:
- If the value of arr[mid] at the current position is equal to key, the search is successful;
- If key is less than the current position value arr[mid], then search for arr[low,mid-1] in the first half of the sequence;
- If key is greater than the current position value arr[mid], continue searching for arr[mid+1,high] in the second half of the sequence until it is found.
Time Complexity:
