Advertisement
cd62131

linear search and binary search

Sep 29th, 2017
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define N (50)
  8. #define UPPER (100)
  9. #define LOWER (0)
  10. #define SEARCHES (5)
  11.  
  12. static int uniform_rand(int, int);
  13. static void array_init_random(int *, size_t, int, int);
  14. static void array_show(int *, size_t);
  15. static void searcher(int *, size_t, int *, size_t, int (*)(int *, size_t, int));
  16. static int linear_search(int *, size_t, int);
  17. static int binary_search(int *, size_t, int);
  18. static int compare_int(const void *, const void *);
  19.  
  20. int main(void) {
  21.   int data[N];
  22.   array_init_random(data, N, LOWER, UPPER);
  23.   puts("data:");
  24.   array_show(data, N);
  25.   int search[SEARCHES];
  26.   array_init_random(search, SEARCHES, LOWER, UPPER);
  27.   puts("search:");
  28.   array_show(search, SEARCHES);
  29.   puts("\nlinear search:");
  30.   searcher(data, N, search, SEARCHES, linear_search);
  31.   qsort(data, N, sizeof(int), compare_int);
  32.   puts("\nsorted data:");
  33.   array_show(data, N);
  34.   puts("binary search:");
  35.   searcher(data, N, search, SEARCHES, binary_search);
  36. }
  37.  
  38. static int uniform_rand(int low, int high) {
  39.   static bool first = true;
  40.   if (first) {
  41.     first = false;
  42.     srand(time(NULL));
  43.   }
  44.   int temp;
  45.   if (low > high) {
  46.     temp = low;
  47.     low = high;
  48.     high = temp;
  49.   }
  50.   return low + rand() % (high - low + 1);
  51. }
  52.  
  53. static void array_init_random(int *a, size_t size, int lower, int upper) {
  54.   for (size_t i = 0; i < size; ++i) {
  55.     a[i] = uniform_rand(lower, upper);
  56.   }
  57. }
  58.  
  59. static void array_show(int *a, size_t size) {
  60.   for (size_t i = 0; i < size; ++i) {
  61.     printf("%3d ", a[i]);
  62.     if (i % 10 == 9) {
  63.       puts("");
  64.     }
  65.   }
  66.   puts("");
  67. }
  68.  
  69. static void searcher(int *target, size_t target_size, int *search,
  70.     size_t search_size, int (*func)(int *, size_t, int)) {
  71.   int sum = 0;
  72.   int found = 0;
  73.   for (size_t i = 0; i < search_size; ++i) {
  74.     int s = search[i];
  75.     printf("search %d: ", s);
  76.     int count = (*func)(target, target_size, s);
  77.     if (count < 0) {
  78.       puts("x");
  79.       continue;
  80.     }
  81.     printf("o %d step\n", count);
  82.     sum += count;
  83.     ++found;
  84.   }
  85.   printf("average: %d step / %d found = %f\n", sum, found,
  86.       (double) sum / found);
  87. }
  88.  
  89. static int linear_search(int *target, size_t size, int search) {
  90.   for (size_t i = 0; i < size; ++i) {
  91.     int t = target[i];
  92.     printf("%d(%zd) ", t, i);
  93.     if (t == search) {
  94.       return i + 1;
  95.     }
  96.   }
  97.   return -1;
  98. }
  99.  
  100. static int binary_search(int *target, size_t size, int search) {
  101.   int count = 1;
  102.   size_t lower = 0;
  103.   size_t upper = size - 1;
  104.   for (size_t mid = lower + (upper - lower) / 2;
  105.       0 <= mid && mid <= size - 1 && lower <= upper;
  106.       mid = lower + (upper - lower) / 2, ++count) {
  107.     int t = target[mid];
  108.     printf("%d(%zd<%zd<%zd) ", t, lower, mid, upper);
  109.     if (t == search) {
  110.       return count;
  111.     }
  112.     if (t > search) {
  113.       upper = mid - 1;
  114.     } else {
  115.       lower = mid + 1;
  116.     }
  117.   }
  118.   return -1;
  119. }
  120.  
  121. static int compare_int(const void *v1, const void *v2) {
  122.   return *(int *) v1 - *(int *) v2;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement