-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs.c
66 lines (63 loc) · 1.12 KB
/
bs.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
#include <stdio.h>
int bs(int *x,int size, int (*compare)(int x,int y));
int lt(int x, int y);
int gt(int x, int y);
//int main(void){
// int i = 0;
// int vals[10];
// int rval;
// for(i=0;i<10;i++){
// vals[i] = 100-i;
// }
// for (i=0;i<10;i++){
// printf("in[%d]=%d\n",i,vals[i]);
// }
// /*HERE: call bs() with the appropriate comparison function */
// rval = bs(vals,10,lt);
// for (i=0;i<10;i++){
// printf("out[%d]=%d\n",i,vals[i]);
// }
// return 0;
//}
int bs(int *x,int size, int (*compare)(int x,int y)){
int i;
int j;
int swapped=0;
int temp;
if (size <1){
return -1;
}
if (x == NULL){
return -1;
}
for (i =0;i<size;i++){
for (j=0;j<size;j++){
if ((*compare)(x[i],x[j])==1){
temp = x[i];
x[i] = x[j];
x[j] = temp;
swapped = 1;
}
}
}
if (swapped == 0){
return -1;
}
return 0;
}
int lt(int x, int y){
if (x <y){
return 1;
}
else{
return 0;
}
}
int gt(int x, int y){
if (x>y){
return 1;
}
else{
return 0;
}
}