-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.c
38 lines (37 loc) · 1.04 KB
/
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
// Simple program to search the entered element in the entered array using binary search technique. The entered array must be in increasing order for this
// code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the number of elements in the array : \n ");
scanf("%d",&n);
printf("\nEnter the elements of the array in increasing order \n ");
int *arr=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int ele;
printf("\nEnter the element to be searched in the array using binary search technique : \n");
scanf("%d",&ele);
int index=0;
int l=0,h=n-1;
int mid = 0;
while(l<=h)
{
mid = (l+h)/2;
if(arr[mid]==ele)
{
index=mid+1;
break;
}
else if(arr[mid]>ele)
h=mid-1;
else l=mid+1;
}
if(index!=0)
printf("\nThe element %d was found in the array at position %d ",ele,index);
else
printf("\nThe element %d was not found in the entered array ",ele);
return 0;
}