What is Binary Search?
Binary search is an algorithm which uses for searching an element on a sorted array. If array is unsort then array needs to be sort first, before using the binary search algorithm on array. You can make it use of some sorting algorithms, such as the selection sort or the quick sort.
What are the cases in Binary Search?
The search starts with comparing the given element with middle element of the array.
#Case 1: In this case if the given element is equal to middle element of array. Then middle position (index) will return.
if(arr[mid] == element)
#Case 2: In this case the given element is less than the middle element of the array. Consider the array is sort in an ascending order. Then the second half of the array will be ignore. And the search for the next middle element continues on the first half.
if(arr[mid] > element)
#Case 3: In this case the process is same as Case 2. But when the given element is greater than the middle element of the array. The first half of the array will be ignore. And the search for the next middle element continues on the second half.
if(arr[mid] < element)
The process repeats until the given element is found. If value of the given element is found then the position of the element in array is returned. Binary Search is fast in comparison to linear search.
Example :-
Array : 4 25 13 8 15 20 10 2 7 5
Search element : 5
Search element : 5
After sorting - 2 4 5 7 8 10 13 15 20 25
1st iteration - 2 4 5 7 8 10 13 15 20 25
mid[5] = 8 | element < mid
2nd iteration- 2 4 5 7 8 10 13 15 20 25
mid[2] = (4) | element > mid
3rd iteration - 2 4 5 7 8 10 13 15 20 25
mid[3] = (5) | element = mid
Result : Element found at position 3.
Steps to implement Binary Search.
- Take value of array elements from user.
- Take value of ele variable from user to be search in the array.
- Sort the array if array is not sort.
- Declare three variables (low, mid, high).
- Initial value of low, mid and high are 0, array length - 1 and (low+high)/2 respectively.
- Use loop and make it's condition low <= high. And code 7,8,9 steps in loop.
- First condition element is equal to mid then break the loop.
- Second condition element is less than mid then initialize high = mid-1.
- Third condition element is greater than mid then initialize low = mid+1.
- After loop termination (break) if mid element is equal to element then display array mid position otherwise display that element is not match
Program to implement Binary Search.
- Program Implement in C
- Implement in C++
- Implement in Java
Recursive program to implement Binary Search.
- Program Recursive program implement in C
- Recursive program implement in C++
- Recursive program implement in Java
Program Implement in C
Output :-
Enter any 10 number >>| 4 25 13 8 15 20 10 2 7 5
Here is the sort elements ==> 2, 4, 5, 7, 8, 10, 13, 15, 20, 25,
Which element do you want to search >>| 5
Element 5 found at 3...
Implement in C++
Output :-
Enter any 10 number >>| 4 25 13 8 15 20 10 2 7 5
Here is the sort elements ==> 2, 4, 5, 7, 8, 10, 13, 15, 20, 25,
Which element do you want to search >>| 5
Element 5 found at 3...
Implement in Java
Output :-
Enter any 10 number >>| 4 25 13 8 15 20 10 2 7 5
Here is the sort elements ==> 2, 4, 5, 7, 8, 10, 13, 15, 20, 25,
Which element do you want to search >>| 5
Element 5 found at 3...
Recursive program implement in C
Output :-
Enter any 10 number >>| 4 25 13 8 15 20 10 2 7 5
Here is the sort elements ==> 2, 4, 5, 7, 8, 10, 13, 15, 20, 25,
Which element do you want to search >>| 5
Element 5 found at 3...
Recursive program implement in C++
Output :-
Enter any 10 number >>| 4 25 13 8 15 20 10 2 7 5
Here is the sort elements ==> 2, 4, 5, 7, 8, 10, 13, 15, 20, 25,
Which element do you want to search >>| 5
Element 5 found at 3...
Recursive program implement in Java
Output :-
Enter any 10 number >>| 4 25 13 8 15 20 10 2 7 5
Here is the sort elements ==> 2, 4, 5, 7, 8, 10, 13, 15, 20, 25,
Which element do you want to search >>| 5
Element 5 found at 3...
Attention readers! Don't stop learning now. Check out our articles to gain more knowledge.
0 Comments
If you have any doubt. Let me know.