-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.c
42 lines (34 loc) · 863 Bytes
/
binary_search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,n,key,mid,arr[100];
printf("Enter the number of numbers in the array : ");
scanf("%d",&n);
int high=n-1,low=0;
printf("\nEnter the numbers[in ascending order] :\n");
for(i=0; i<n; i++) //reading the elements
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to be searched : ");//getting the element to be searched
scanf("%d",&key);
//binary search
do
{
mid=(low+high)/2 ;
if ( key < arr[mid] )
high = mid -1;
else if ( key > arr[mid])
low = mid + 1;
}while(key!=arr[mid] && low <= high);//till here
if(key==arr[mid])//result printing
{
printf("\n\nSUCCESSFUL SEARCH :)\n\n");
}
else
{
printf("\n\nSEARCH FAILED :(\n\n");
}
return 0;
}