-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryintarray.c
72 lines (54 loc) · 1.42 KB
/
binaryintarray.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
static inline int binaryAdd(int *v, int size, int val) {
int i, j;
for (i = 0; i < size; i++) {
if (val < v[i] || val == v[i]) {
int prec, temp;
temp = val;
for (j = i; j < size + 1; j++) { // shift
prec = v[j];
v[j] = temp;
temp = prec;
}
return i;
}
}
v[size] = val;
return size;
}
static inline int unsortedSearch(int *v, int size, int val) {
// Start searching from both ends
int front = 0, back = size - 1;
// Keep searching while two indexes
// do not cross.
while (front <= back) {
if (v[front] == val) return front;
if (v[back] == val) return back;
front++;
back--;
}
return -1;
}
static inline int binarySearch(int *v, int size, int val) {
int l = 0, r = size - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (val == v[m])
return m;
// If x greater, ignore left half
if (val > v[m])
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
return -1;
}
static inline int binaryDelete(int *v, int size, int val) {
int i = binarySearch(v, size, val);
if (i < 0)
return 0;
for (; i < size - 1; i++)
v[i] = v[i+1];
return 1;
}