-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_search.c
76 lines (70 loc) · 1.33 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
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
#include<stdio.h>
void main()
{
int i,j,n,num,temp,c=0;
c+=2;
printf("Enter the array size : ");
scanf("%d",&n);
int a[n];
printf("Enter the array elements : ");
for(i=0;i<n;i++)
{
c++;
scanf("%d",&a[i]);
c++;
}c++;
for(i=0;i<n;i++)
{
c++;
for(j=0;j<n-i-1;j++)
{
c++;
if(a[j]>a[j+1])
{
c+=2;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}c++;
}c++;
printf("sorted array is : ");
for(i=0;i<n;i++)
{
c++;
printf("%d",a[i]);
c++;
}c++;
int l=0,r=n-1;
c+=2;
printf("\nEnter the number u want to search : ");
scanf("%d",&num);
while(l<=r)
{
c++;
int mid=(l+r)/2;
if(a[mid]==num)
{
c++;
printf("Element is found at index %d\n ",mid);
break;
}
else if(a[mid]>num)
{
c++;
r=mid-1;
}
else if(a[mid]<num)
{
c++;
l=mid+1;
}
}
if(l>r)
{
c++;
printf("Element is not found ");
}
printf("time complexity is %d\n",c);
printf("Space complexity is %d",24+(4*n));
}