-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path1830122_exp4_2.c
74 lines (67 loc) · 1.39 KB
/
1830122_exp4_2.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
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
int binary(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binary(arr, l, mid - 1, x);
return binary(arr, mid + 1, r, x);
}
return -1;
}
void ascending(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int n,i,x,c;
printf("Enter the size of the array ");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
a[i]=(rand() % (1000) + 1);
}
printf("The numbers assigned are : \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("Element at middle position : %d\n",a[(n-1)/2]);
printf("Enter the element to be searched : ");
scanf("%d",&x);
ascending(a,n);
clock_t start,end;
start=clock();
c=binary(a,0,n-1,x);
if(c==-1)
{
printf("Element not found\n");
}
else
{
printf("Element found\n");
}
end=clock();
double time_taken=(((double)(end-start))/CLOCKS_PER_SEC);
printf("The time taken to execute is %f seconds",time_taken);
return 0;
}