-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search.c
More file actions
60 lines (60 loc) · 845 Bytes
/
binary_search.c
File metadata and controls
60 lines (60 loc) · 845 Bytes
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
49
50
51
52
53
54
55
56
57
58
59
60
#include<conio.h>
#include<stdio.h>
#define size 7
void sort(int *p,int x);
int search(int *,int,int);
void main()
{
int a[size],i;
for(i=0;i<size;i++)
scanf("%d",&a[i]);
sort(&a[0],size);
for(i=0;i<size;i++)
printf("%d ",a[i]);
printf("enter element...");
scanf("%d",&i);
if(i=search(&a[0],size,i))
printf("place of element is %d",i);
getch();
}
void sort(int *p,int x)
{
int i,j,k,t;
for(i=0;i<x-1;i++)
{
k=i+1;
for(j=i+2;j<x;j++)
if(*(p+k)>*(p+j))
k=j;
if(*(p+i)>*(p+k))
{
t=*(p+k);
*(p+k)=*(p+i);
*(p+i)=t;
}
}
}
int search(int *a,int l,int s)
{
int f=0,m=3;
for(;1;)
{
if(*(a+m)<s)
{
f=m+1;
m=(f+l)/2;
}
else if(*(a+m)>s)
{
l=m-1;
m=(f+1)/2;
}
else if(*(a+m)==s)
return m+1;
if(f>l)
{
printf("element not found....");
return 0;
}
}
}