-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBINARYSE.C
More file actions
78 lines (71 loc) · 1.24 KB
/
BINARYSE.C
File metadata and controls
78 lines (71 loc) · 1.24 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<conio.h>
void readarray(int a[50],int n)
{
int i;
printf("\nEnter Elements : ");
for(i=0;i<n;i++)
{
scanf("\n%d",&a[i]);
}
}
void printarr(int a[50],int n){
int i;
printf("\nElements\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
int binary(int a[100],int l,int h,int key){
int mid;
if(l<=h)
{
mid=(l+h)/2;
if(key==a[mid])
return mid;
else
if(a[mid]>key)
return binary(a,l,mid-1,key);
else
return binary(a,mid+1,h,key);
}
return -1;
}
void bubblesort(int a[50],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
void main() {
int n,ch=1,a[100],key,b;
clrscr();
printf("\nEnter Size : ");
scanf("%d",&n);
readarray(a,n);
bubblesort(a,n);
printarr(a,n);
while(ch){
printf("\nEnter Search : ");
scanf("%d",&key);
b=binary(a,0,n-1,key);
if(b!=-1)
printf("\nKey Found");
else printf("\nNot Found");
printf("\nEnter Option : ");
scanf("%d",&ch);
}
getch();
}