-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab 4_Prog 1.c
62 lines (61 loc) · 1.32 KB
/
Lab 4_Prog 1.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int n;
int binarySearch(int arr[], int l, int h, int key)
{
int mid;
while (l <= h)
{
mid = (l + h) / 2;
if (key == arr[mid])
{
return 1;
}
else if (key < arr[mid])
{
h = mid - 1;
}
else
{
l = mid + 1;
}
}
return -1;
}
int main()
{
printf("\nEnter the size of the array : ");
scanf("%d", &n);
int arr[n], i;
for (i = 0; i < n; i++)
arr[i] = rand() % 100;
printf("\nThe array is : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
mergeSort(arr, 0, n - 1);
int key;
printf("\nThe sorted array is : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
double total = 0;
for (int i = 0; i < 5; i++)
{
printf("\nEnter the key to be searched : \n");
scanf("%d", &key);
clock_t start, end;
double cpu_time_used;
start = clock();
sleep(1);
if (binarySearch(arr, 0, n - 1, key) == 1)
printf("Key Found");
else
printf("Key not found");
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
total += cpu_time_used;
printf("\nTime taken : %0.6f", cpu_time_used);
}
printf("\nAverage case time complexity is %0.6f\n", total / 5.000000);
return 0;
}