-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsearch_run.c
94 lines (77 loc) · 1.99 KB
/
bsearch_run.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
/*
* File: bsearch_run.c
* Author: prakbans
* Created on September 21, 2014, 2:17 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "bsearch.h"
void integerSortAndSearch();
void stringSortAndSearch();
struct mi {
int nr;
char *name;
} months[] = {
{ 1, "jan"},
{ 2, "feb"},
{ 3, "mar"},
{ 4, "apr"},
{ 5, "may"},
{ 6, "jun"},
{ 7, "jul"},
{ 8, "aug"},
{ 9, "sep"},
{10, "oct"},
{11, "nov"},
{12, "dec"}
};
#define nr_of_months (sizeof(months)/sizeof(months[0]))
static int
compmi(const void *m1, const void *m2) {
struct mi *mi1 = (struct mi *) m1;
struct mi *mi2 = (struct mi *) m2;
return strcmp(mi1->name, mi2->name);
}
int nums[] = {12, 15, 17, 19, 22, 67};
#define nr_of_ints (sizeof(nums)/sizeof(nums[0]))
static int
compmi_int(const void *m1, const void *m2) {
int *mi1 = (int *) m1;
int *mi2 = (int *) m2;
if (*mi1 < *mi2) return -1;
else if (*mi1 > *mi2) return 1;
else return 0;
}
/*
*
*/
int main(int argc, char** argv) {
integerSortAndSearch();
return (EXIT_SUCCESS);
}
void integerSortAndSearch() {
int key = 120;
int64_t *k = binarySearch(&key, nums, nr_of_ints,
sizeof (int), compmi_int);
int* res = (int*) k;
if (res == NULL)
printf("aq%saq: unknown month\n", "FAILURE HAHA");
else
printf("integer #%d\n", *res);
}
void stringSortAndSearch() {
int i;
qsort(months, nr_of_months, sizeof (struct mi), compmi);
for (i = 1; i < 2; i++) {
struct mi key;
key.name = "jan";
int64_t *k = binarySearch(&key, months, nr_of_months,
sizeof (struct mi), compmi);
printf("The value of k is %lx", k);
struct mi* res = (struct mi*) k;
if (res == NULL)
printf("aq%saq: unknown month\n", "FAILURE HAHA");
else
printf("%s: month #%d\n", res->name, res->nr);
}
}