Binary search program | Binary Search in c | Binary search example
In this article Binary search program we give the information about binary search example, binary search tree in data structure, binary search program in c and Binary search in data structure.
Binary Search in c:
Binary search is most powerful and efficient searching method. The binary search method can be use only with the data, which is already in the sorted order.
In this method, the given element that is to be search is compared with the middle element of given array.
There are three cases in this method.
- If the given element for searching is same as middle element of an array then search is successful.
- If the given element for searching is less than a middle element of an array, then searching is done only is first half of array.
- If the given element for searching is greater than a middle element of an array, then searching is done only in second half of array.
|First Half of an array||Middle element of an array||Second half of an array|
Binary Search Algorithm:
Step 1: Start
2: Read n elements of an array arr in sorted order Or Read n elements of an array arr and sort the elements.
3: Read element Y to be search
4: set beg=0 and end =n-1
5: Calculate mid = (beg + end)/ 2;
6: if Y=arr[mid] then
Display “Element is found…..search successful ” and go to step 10.
7: if Y<arr[mid] then
beg = mid + 1
8: if beg < = end then go to step 5
9: Display “ Element not found ….., search Unsuccessful”.
Binary Search Example:
Consider the elements of an array as follows
The element to be search is 53.
Iteration – 1:
Beg=0, end=9, mid=(0 + 9) / 2 = 4
As the mid is 4, therefore the middle element is 28.
Compare middle element 28 with 53.
Since 53 > 28, Search the element in the right portion of the array.
Hence, now beg = mid + 1, i.e. 4 + 1 = 5
There is no change in the value of end.
Iteration – 2:
Beg = 5, end =9, mid = ( 5 + 9 ) / 2 = 7
As the mid is 7, therefore the middle element is 62.
Compare middle element 62 with 53.
Since 53 < 62, search the element in the left portion of the array.
Hence, now end = mid = 7. There is no change in the value of beg.
Iteration – 3:
Beg = 5, end = 7, mid = ( 5 + 7 ) / 2 = 6
Here we find the index of element to be search. Now we can access the value using index. Thus, search is successful.
Binary Search Program:
int beg=0,end=10,mid=0,n=0, flag=0;
printf(“\n Enter element to be search : ”);
while((beg<=end) && (n!=arr[mid]))
printf(“\n Search is successful…”);
printf(“\n Search is not successful…”);
Enter element to be search : 25
Search is not successful…
- To search a particular record in database.
- To retrieve a data from storage device speedily and efficiently.
- Data Structure using C | Data structures concepts in C
- Bubble sort in data structure | Bubble sort algorithm in C
- Insertion sort in c | Insertion sort algorithm | Insertion sort example
- Selection sort in c | Selection sort algorithm | Selection sort example
- Quick sort in C | Quick sort example | Quick sort program in C