-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_search.c
192 lines (149 loc) · 3.99 KB
/
demo_search.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include "ctlib/ctl.c"
/*
Binary Search Demo:
- The user defines custom functions and utility functions that the library can use.
- Here we show a simple binary search application using an int array and the compare function
*/
int demo_compare(ctemplate a, ctemplate b)
{
// User knows that the array is an int array.
// Thus, user first parses the ctemplates to obtain the integer values.
int a1 = (* ((int*) a.ptr));
int a2 = (* ((int*) b.ptr));
// The library requires the compare function to yield 0, if the values match,
// Otherwise, it must return negative or positive based on the comparison
if(a1==a2)
{
return 0;
}
else if(a1 < a2)
{
return -1;
}
else // that is, if(a1 > a2)
{
return 1;
}
}
void demo_assign(ctemplate a, ctemplate b)
{
* (int*) a.ptr = * (int*) b.ptr;
}
void demo_binary_search(int * a, int N, int key)
{
// Create a ctemplate for a - to be used by the library
ctemplate t_a = {a, sizeof(int)};
ctl_size_t t_N = N;
ctemplate t_key = {&key, sizeof(int)};
// Run the binary search algorithm
ctl_size_t pos = ctl_binary_search(t_a, t_N, t_key, &demo_compare);
if(pos == N)
{
printf("Number %d not found\n", key);
}
else
{
printf("Found number %d at position: %ld\n", key, pos);
}
}
void demo_linear_search(int * a, int N, int key)
{
// Create a ctemplate for a - to be used by the library
ctemplate t_a = {a, sizeof(int)};
ctl_size_t t_N = N;
ctemplate t_key = {&key, sizeof(int)};
// Run the binary search algorithm
ctl_size_t pos = ctl_linear_search(t_a, t_N, t_key, &demo_compare);
if(pos == N)
{
printf("Number %d not found\n", key);
}
else
{
printf("Found number %d at position: %ld\n", key, pos);
}
}
void demo_linear_search_transpose(int * a, int N, int key)
{
// Create a ctemplate for a - to be used by the library
ctemplate t_a = {a, sizeof(int)};
ctl_size_t t_N = N;
ctemplate t_key = {&key, sizeof(int)};
// Run the binary search algorithm
ctl_size_t pos = ctl_linear_search_transpose(t_a, t_N, t_key, &demo_compare, &demo_assign);
if(pos == N)
{
printf("Number %d not found\n", key);
}
else
{
printf("Found number %d at position: %ld\n", key, pos);
}
}
void demo_linear_search_transpose_exponential(int * a, int N, int key)
{
// Create a ctemplate for a - to be used by the library
ctemplate t_a = {a, sizeof(int)};
ctl_size_t t_N = N;
ctemplate t_key = {&key, sizeof(int)};
// Run the binary search algorithm
ctl_size_t pos = ctl_linear_search_transpose_exponential(t_a, t_N, t_key, &demo_compare, &demo_assign);
if(pos == N)
{
printf("Number %d not found\n", key);
}
else
{
printf("Found number %d at position: %ld\n", key, pos);
}
}
void demo_is_sorted(int * a, int N)
{
if (ctl_is_sorted((ctemplate){a, sizeof(int)}, N, &demo_compare))
{
printf("Array is sorted.\n");
}
else
{
printf("Array is not sorted.\n");
}
}
void plot(int * a, int N)
{
printf("Array Is: ");
for(int i=0; i<N; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
void nl()
{
printf("\n");
}
void demo_reverse(int * a, int N)
{
printf("Reversing list.\n");
ctl_reverse((ctemplate){a, sizeof(int)}, N, &demo_assign);
}
void demo_rotate_inplace(int * a, int N, int c)
{
printf("Rotating the list by %d\n", c);
ctl_rotate_inplace((ctemplate){a, sizeof(int)}, N, c, &demo_assign);
}
int main()
{
// Create an array
int a[] = {1, 6, 7, 15, 18, 24, 39, 45, 50, 62, 88, 100};
int N = sizeof(a) / sizeof(int);
plot(a, N); demo_is_sorted(a, N); nl();
demo_binary_search(a, N, 39); plot(a, N); demo_is_sorted(a, N); nl();
demo_binary_search(a, N, 25); plot(a, N); demo_is_sorted(a, N); nl();
demo_linear_search(a, N, 88); plot(a, N); demo_is_sorted(a, N); nl();
demo_linear_search_transpose(a, N, 18); plot(a, N); demo_is_sorted(a, N); nl();
demo_linear_search_transpose_exponential(a, N, 24); plot(a, N); demo_is_sorted(a, N); nl();
demo_reverse(a, N); plot(a, N); nl();
demo_rotate_inplace(a, N, -3); plot(a, N); nl();
demo_rotate_inplace(a, N, 4); plot(a, N); nl();
}