-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-1.c
93 lines (70 loc) · 1.7 KB
/
3-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
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
/*
Exercise 3-1.
Our binary search makes two tests inside the loop,
when one would suffice (at the price of more tests outside).
Write a version with only one test inside the loop and measure the difference in run-time.
Compare this runtime
real 0m0.004s
user 0m0.001s
sys 0m0.002s
3-1-2.c -- 2 conditions on each loop
real 0m0.005s
user 0m0.002s
sys 0m0.003s
3-1alt.c
real 0m0.004s
user 0m0.001s
sys 0m0.002s
*/
/* todo: time ./a.out to check runtime */
#include <stdio.h>
int binsearch2 (int x, int v[], int n);
int main(){
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]); /* https://hackr.io/blog/binary-search-in-c */
int x = 10;
int result = binsearch2(x, arr, n);
(result == -1) ? printf("The element is not present in array \n")
: printf("The element is present at index %d \n", result);
return 0;
}
/*
binsearch:find x in v[O]<=v[1]<= ••• <=v[n-1]
*/
int binsearch1 (int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid =(low+high) / 2;
if (x < v[mid])
high = mid - 1;
else if (x > v[mid])
low =mid + 1;
else /* foundmatch */
return mid;
}
return -1; /*nomatch*/
}
/*
one condition within loop
*/
int binsearch2 (int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high && v[mid] != x) {
mid =(low+high) / 2;
if (x < v[mid])
high = mid - 1;
else /* (x > v[mid])*/
low =mid + 1;
}
if(v[mid] == x){
return mid;
}
else
return -1; /*nomatch*/
}