-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.c
117 lines (106 loc) · 1.95 KB
/
BinarySearch.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
clock_t start, end;
int arr[100000];
double cpu_time;
void printArray(int arr[], int n)
{
for(int i = 0; i<n; i++)
printf("%d\t", arr[i]);
printf("\n");
}
void binarySearch(int arr[], int n, int key)
{
int low = 0;
int high = n-1;
while(low<=high)
{
int mid = (low+high)/2;
if(arr[mid] == key)
{
printf("Key found at %d location.\n", mid+1);
return;
}
else if(key > arr[mid])
{
low=mid+1;
}
else
{
high = mid-1;
}
}
printf("Key not found.\n");
}
void merge(int arr[], int left[], int right[], int p, int q)
{
int i=0, j=0,k=0;
while(i<p && j<q)
{
if(left[i]<right[j])
{
arr[k] = left[i];
i += 1;
}
else
{
arr[k] = right[j];
j += 1;
}
k+=1;
}
while(i<p)
{
arr[k] = left[i];
i += 1;
k+= 1;
}
while(j<q)
{
arr[k] = right[j];
j +=1 ;
k += 1;
}
}
void mergeSort(int arr[], int n)
{
if(n<=1)
{
return;
}
int left[n/2];
int right[n-n/2];
for(int i=0;i<n/2;i++)
{
left[i] = arr[i];
}
for(int j=0;j<n-n/2;j++)
{
right[j] = arr[j+n/2];
}
mergeSort(left, n/2);
mergeSort(right, n-n/2);
merge(arr,left,right,n/2, n-n/2);
}
int main()
{
int n,i;
printf("Enter the size of array: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
arr[i] = rand()%9001+1000;
}
mergeSort(arr,n);
printf("The sorted array is: ");
printArray(arr,n);
int key;
printf("Enter the key: ");
scanf("%d", &key);
start = clock();
binarySearch(arr,n,key);
end = clock();
cpu_time = (double)(end-start)/CLOCKS_PER_SEC;
printf("The time taken is: %lf\n", cpu_time);
}