-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIN_SER.C
More file actions
60 lines (60 loc) · 972 Bytes
/
BIN_SER.C
File metadata and controls
60 lines (60 loc) · 972 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<stdio.h>
#include<conio.h>
void main()
{
int arr[10],i,j,t,l=0,h=0,mid=0,ser,pos=0;
clrscr();
printf("The limit of the array is ten elements.\n");
printf("Enter the elements.\n");
for(i=0;i<10;i++)
{
printf("Element %d:\t",i+1);
scanf("%d",&arr[i]);
}
//sorting
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(arr[i]<arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
printf("\n\nThe sorted list is:\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);
printf("Enter a value to be searched:\t");
scanf("%d",&ser);
// binary search
l=0;h=9;
while(l<=h && pos==0)
{
mid=(l+h)/2;
if(ser==arr[mid])
{
pos=mid+1;
break;
}
else if(ser<arr[mid])
{
h=mid-1;
}
else if(ser>arr[mid])
{
l=mid+1;
}
}
if(pos!=0)
{
printf("\n\n%d is found at position %d",ser,pos);
}
else
{
printf("\n\n %d is not found in the list",ser);
}
getch();
}