-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinrec.c
48 lines (36 loc) · 797 Bytes
/
binrec.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
43
44
45
46
47
48
#include<stdio.h>
void sort(int *ar,int n){
int temp;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(ar[j]>ar[j+1]){
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
}
void binsearch(int *ar,int low,int upp,int key){
if(low>upp){
printf("Element not found");
return;
}
int mid=(low+upp)/2;
if(key>ar[mid]) binsearch(ar,mid+1,upp,key);
else if(key<ar[mid]) binsearch(ar,low,mid-1,key);
else printf("Element found at position %d",mid+1);
}
void main(){
int arr[100],i,n,key;
printf("Enter the size of the array(<50):");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter element %d:",i+1);
scanf("%d",&arr[i]);
}
sort(arr,n);
printf("Enter the element to be searched:");
scanf("%d",&key);
binsearch(arr,0,n-1,key);
}