Binary Search in Python
Binary Search in Python
Binary Search is a searching algorithm for finding an element's position in a sorted array.
In this approach, the element is always searched in the middle of a portion of an array.
Binary Search Algorithm can be implemented in two ways which are discussed below.
1. Iterative Method
2. Recursive Method
2. Set two pointers low and high at the lowest and the highest positions respectively.
3. Find the middle element mid of the array ie. arr[(low + high)/2] = 6.
4. If x == mid, then return mid.Else, compare the element to be searched with m.
5. If x > mid, compare x with the middle element of the elements on the right side of mid.
This is done by setting low to low = mid + 1.
6. Else, compare x with the middle element of the elements on the left side of mid. This
is done by setting high to high = mid - 1.
7.
9. x = 4 is found.