-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch24s05.c
More file actions
94 lines (77 loc) · 1.82 KB
/
ch24s05.c
File metadata and controls
94 lines (77 loc) · 1.82 KB
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
#include <stdio.h>
#define LEN 5
typedef void (*sort)(int, void *);
typedef int (*cmp)(const void *, const int, const double);
void double_sort(int j, void *arr)
{
double *array = (double *)arr;
double key = array[j];
int i = j - 1;
while (i >= 0 && array[i] > key) {
array[i+1] = array[i];
i--;
}
array[i+1] = key;
}
void int_sort(int j, void *arr)
{
int *array = (int *)arr;
int key = array[j];
int i = j - 1;
while (i >= 0 && array[i] > key) {
array[i+1] = array[i];
i--;
}
array[i+1] = key;
}
void insertion_sort(void *array, sort sort)
{
int j;
for (j = 1; j < LEN; j++) {
sort(j, array);
}
}
sort *sort_double = double_sort;
sort *sort_int = int_sort;
/*--------------------------------------------------*/
int double_cmp(const void * arr, const int subscript, const double b)
{
double *array = (double *)arr;
double diff = array[subscript] - b;
return (diff > 0) - (diff < 0);
}
int int_cmp(const void * arr, const int subscript, const double b)
{
int *array = (int *)arr;
return (array[subscript] > (int)b) - (array[subscript] < (int)b);
}
int binarysearch(double number, void *array, cmp cmp)
{
int mid, start = 0, end = LEN - 1;
// printf("%d\n", (int)number);
while (start <= end) {
mid = (start + end) / 2;
if (cmp(array, mid, number) == -1)
start = mid + 1;
else if (cmp(array, mid, number) == 1)
end = mid - 1;
else
return mid;
}
return -1;
}
cmp *cmp_double = double_cmp;
cmp *cmp_int = int_cmp;
/*--------------------------------------------------*/
int main(void)
{
int a[LEN] = { 1, 2, 2, 5, 7 };
double b[LEN] = { 4.2, 5.0, 7.4, 7.6, 8.0 };
// insertion_sort((void *)b, sort_double);
// for (size_t i = 0; i < LEN; i++) {
// printf("%f ", b[i]);
// }
// printf("\n");
printf("%d\n", binarysearch(8, (void *)b, double_cmp));
return 0;
}