new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
36
Task/Binary-search/C/binary-search-1.c
Normal file
36
Task/Binary-search/C/binary-search-1.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* http://www.solipsys.co.uk/b_search/spec.htm */
|
||||
typedef int Object;
|
||||
|
||||
int cmpObject(Object* pa, Object *pb)
|
||||
{
|
||||
Object a = *pa;
|
||||
Object b = *pb;
|
||||
if (a < b) return -1;
|
||||
if (a == b) return 0;
|
||||
if (a > b) return 1;
|
||||
assert(0);
|
||||
}
|
||||
|
||||
int bsearch(Object Array[], int n, Object *KeyPtr,
|
||||
int (*cmp)(Object *, Object *),
|
||||
int NotFound)
|
||||
{
|
||||
unsigned left = 1, right = n; /* `unsigned' to avoid overflow in `(left + right)/2' */
|
||||
|
||||
if ( ! (Array && n > 0 && KeyPtr && cmp))
|
||||
return NotFound; /* invalid input or empty array */
|
||||
|
||||
while (left < right)
|
||||
{
|
||||
/* invariant: a[left] <= *KeyPtr <= a[right] or *KeyPtr not in Array */
|
||||
unsigned m = (left + right) / 2; /*NOTE: *intentionally* truncate for odd sum */
|
||||
|
||||
if (cmp(Array + m, KeyPtr) < 0)
|
||||
left = m + 1; /* a[m] < *KeyPtr <= a[right] or *KeyPtr not in Array */
|
||||
else
|
||||
/* assert(right != m) or infinite loop possible */
|
||||
right = m; /* a[left] <= *KeyPtr <= a[m] or *KeyPtr not in Array */
|
||||
}
|
||||
/* assert(left == right) */
|
||||
return (cmp(Array + right, KeyPtr) == 0) ? right : NotFound;
|
||||
}
|
||||
38
Task/Binary-search/C/binary-search-2.c
Normal file
38
Task/Binary-search/C/binary-search-2.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#define DUMMY -1 /* dummy element of array (to adjust indexing from 1..n) */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Object a[] = {DUMMY, 0, 1, 1, 2, 5}; /* allowed indices from 1 to n including */
|
||||
int n = sizeof(a)/sizeof(*a) - 1;
|
||||
const int NotFound = -1;
|
||||
|
||||
/* key not in Array */
|
||||
Object key = 4;
|
||||
assert(NotFound == bsearch(a, n, &key, cmpObject, NotFound));
|
||||
key = DUMMY;
|
||||
assert(NotFound == bsearch(a, n, &key, cmpObject, NotFound));
|
||||
key = 7;
|
||||
assert(NotFound == bsearch(a, n, &key, cmpObject, NotFound));
|
||||
|
||||
/* all possible `n' and `k' for `a' array */
|
||||
int k;
|
||||
key = 10; /* not in `a` array */
|
||||
for (n = 0; n <= sizeof(a)/sizeof(*a) - 1; ++n)
|
||||
for (k = n; k>=1; --k) {
|
||||
int index = bsearch(a, n, &a[k], cmpObject, NotFound);
|
||||
assert(index == k || (k==3 && index == 2) || n == 0); /* for equal `1's */
|
||||
assert(NotFound == bsearch(a, n, &key, cmpObject, NotFound));
|
||||
}
|
||||
n = sizeof(a)/sizeof(*a) - 1;
|
||||
|
||||
/* NULL array */
|
||||
assert(NotFound == bsearch(NULL, n, &key, cmpObject, NotFound));
|
||||
/* NULL &key */
|
||||
assert(NotFound == bsearch(a, n, NULL, cmpObject, NotFound));
|
||||
/* NULL cmpObject */
|
||||
assert(1 == bsearch(a, n, &a[1], cmpObject, NotFound));
|
||||
assert(NotFound == bsearch(a, n, &a[1], NULL, NotFound));
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
21
Task/Binary-search/C/binary-search-3.c
Normal file
21
Task/Binary-search/C/binary-search-3.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <stdlib.h> /* for bsearch */
|
||||
#include <stdio.h>
|
||||
|
||||
int intcmp(const void *a, const void *b)
|
||||
{
|
||||
/* this is only correct if it doesn't overflow */
|
||||
return *(const int *)a - *(const int *)b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int nums[5] = {2, 3, 5, 6, 8};
|
||||
int desired = 6;
|
||||
int *ptr = bsearch(&desired, nums, 5, sizeof(int), intcmp);
|
||||
if (ptr == NULL)
|
||||
printf("not found\n");
|
||||
else
|
||||
printf("index = %d\n", ptr - nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue