This article was automatically translated from the original Turkish version.
+1 More
Binary Search is a search algorithm used to find the position of an element in a sorted array. In this approach, the element is searched at the midpoint of a segment of the array.
Binary search can only be applied to a sorted list of elements. If the elements are not already sorted, they must be sorted first.
The Binary Search algorithm can be implemented in two ways.
Although the recursive approach adopts the divide and conquer logic, both methods generally follow these steps:
Step 1:
The array is in sorted order and the target element is 4.
Step 2:
The smallest and largest elements of the array are identified.
Step 3:
The middle element of the array is determined.
middle element = (smallest element + largest element) / 2
Step 4: If the target element is equal to the middle element, the element has been found. If not, the found element is compared with the target element.
Step 5: If the target element is greater than the middle element, the search continues among the elements to the right of the middle element. In this case, the smallest element is set to middle element + 1.
Step 6: If the target element is smaller than the middle element, the search continues among the elements to the left of the middle element. In this case, the largest element is set to middle element - 1.
Step 7: The search continues until there is only one element (the target) remaining between the smallest and largest elements.
No Discussion Added Yet
Start discussion for "Binary Search Algorithm" article
Working Principle
Binary Search Algorithm Iterative Approach Pseudocode
Binary Search Algorithm Recursive Approach Pseudocode
Binary Search Algorithm with Java Code