Binary search algorithm is a popular search algorithm used to search for a specific element in a sorted list or array. It is an efficient algorithm that works by dividing the list into halves until the desired element is found.
How Binary Search Algorithm Works
Binary search algorithm works by dividing the list into halves and checking if the middle element is equal to the desired element. If the middle element is greater than the desired element, the search is continued in the first half of the list. If the middle element is smaller than the desired element, the search is continued in the second half of the list. This process is repeated until the desired element is found, or the search area is reduced to zero.
Python Implementation of Binary Search Algorithm
Here’s an example of implementing the binary search algorithm in Python:
def binary_search(arr, x):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
left = mid + 1
else:
right = mid - 1
return -1
In the above code, the binary_search
function takes two arguments, arr
and x
, where arr
is the list or array to be searched, and x
is the element to be searched for. The function uses a while
loop to continuously divide the list into halves until the desired element is found or the search area is reduced to zero. At each iteration, the function checks if the middle element is equal to the desired element. If the middle element is greater than the desired element, the search continues in the first half of the list. If the middle element is smaller than the desired element, the search continues in the second half of the list. The loop terminates when the desired element is found, or the search area is reduced to zero.
Example of Binary Search Algorithm
Let’s say we have a list of numbers as follows:
arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
We want to search for the element 50 in this list using the binary search algorithm. We can do this by calling the binary_search
function with the list arr
and the element 50
as arguments:
index = binary_search(arr, 50)
After executing this code, the variable index
will contain the value 4
, which is the index of the element 50
in the list.
Advantages and Disadvantages of Binary Search Algorithm
The binary search algorithm has the following advantages and disadvantages:
Advantages:
- Efficient for large datasets.
- Time complexity is O(log n) where n is the number of elements in the list or array.
Disadvantages:
- Only works on sorted lists or arrays.
- More complex to implement than linear search.
Conclusion
In conclusion, binary search algorithm is an efficient search algorithm used to find a specific element in a sorted list or array. It works by dividing the list into halves and checking if the middle element is equal to the desired element. Although it has some limitations, it is still useful for large datasets or when the list is sorted.
For complete list of topic on DATA STRUCTURE AND ALGORITHM click hear