Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define N (50)
- #define UPPER (100)
- #define LOWER (0)
- #define SEARCHES (5)
- static int uniform_rand(int, int);
- static void array_init_random(int *, size_t, int, int);
- static void array_show(int *, size_t);
- static void searcher(int *, size_t, int *, size_t, int (*)(int *, size_t, int));
- static int linear_search(int *, size_t, int);
- static int binary_search(int *, size_t, int);
- static int compare_int(const void *, const void *);
- int main(void) {
- int data[N];
- array_init_random(data, N, LOWER, UPPER);
- puts("data:");
- array_show(data, N);
- int search[SEARCHES];
- array_init_random(search, SEARCHES, LOWER, UPPER);
- puts("search:");
- array_show(search, SEARCHES);
- puts("\nlinear search:");
- searcher(data, N, search, SEARCHES, linear_search);
- qsort(data, N, sizeof(int), compare_int);
- puts("\nsorted data:");
- array_show(data, N);
- puts("binary search:");
- searcher(data, N, search, SEARCHES, binary_search);
- }
- static int uniform_rand(int low, int high) {
- static bool first = true;
- if (first) {
- first = false;
- srand(time(NULL));
- }
- int temp;
- if (low > high) {
- temp = low;
- low = high;
- high = temp;
- }
- return low + rand() % (high - low + 1);
- }
- static void array_init_random(int *a, size_t size, int lower, int upper) {
- for (size_t i = 0; i < size; ++i) {
- a[i] = uniform_rand(lower, upper);
- }
- }
- static void array_show(int *a, size_t size) {
- for (size_t i = 0; i < size; ++i) {
- printf("%3d ", a[i]);
- if (i % 10 == 9) {
- puts("");
- }
- }
- puts("");
- }
- static void searcher(int *target, size_t target_size, int *search,
- size_t search_size, int (*func)(int *, size_t, int)) {
- int sum = 0;
- int found = 0;
- for (size_t i = 0; i < search_size; ++i) {
- int s = search[i];
- printf("search %d: ", s);
- int count = (*func)(target, target_size, s);
- if (count < 0) {
- puts("x");
- continue;
- }
- printf("o %d step\n", count);
- sum += count;
- ++found;
- }
- printf("average: %d step / %d found = %f\n", sum, found,
- (double) sum / found);
- }
- static int linear_search(int *target, size_t size, int search) {
- for (size_t i = 0; i < size; ++i) {
- int t = target[i];
- printf("%d(%zd) ", t, i);
- if (t == search) {
- return i + 1;
- }
- }
- return -1;
- }
- static int binary_search(int *target, size_t size, int search) {
- int count = 1;
- size_t lower = 0;
- size_t upper = size - 1;
- for (size_t mid = lower + (upper - lower) / 2;
- 0 <= mid && mid <= size - 1 && lower <= upper;
- mid = lower + (upper - lower) / 2, ++count) {
- int t = target[mid];
- printf("%d(%zd<%zd<%zd) ", t, lower, mid, upper);
- if (t == search) {
- return count;
- }
- if (t > search) {
- upper = mid - 1;
- } else {
- lower = mid + 1;
- }
- }
- return -1;
- }
- static int compare_int(const void *v1, const void *v2) {
- return *(int *) v1 - *(int *) v2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement