Advertisement
informaticage

Binary search rec and it implementation

May 23rd, 2021
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define LEN 16
  7.  
  8. bool binary_search(int v[], size_t length, int to_find);
  9. bool binary_search_rec(int v[], size_t length, int to_find, size_t i, size_t j);
  10. bool binary_search_rec_2(int v[], size_t length, int to_find, size_t i,
  11.                          size_t j) int sum(int v[], size_t length);
  12. int sum_rec(int v[], size_t length, size_t index);
  13.  
  14. int main(void) {
  15.   int vtest[LEN];
  16.   srand(time(NULL));
  17.   for (size_t i = 0; i < LEN; i++) {
  18.     vtest[i] = rand() % 30 + 1;
  19.   }
  20.  
  21.   // Simple sorting implementation
  22.   for (size_t i = 0; i < LEN; i++) {
  23.     for (size_t j = i + 1; j < LEN; j++) {
  24.       if (vtest[i] > vtest[j]) {
  25.         int temp = vtest[i];
  26.         vtest[i] = vtest[j];
  27.         vtest[j] = temp;
  28.       }
  29.     }
  30.   }
  31.  
  32.   // Printing the resulting array
  33.   printf("[ ");
  34.   for (size_t i = 0; i < LEN; i++) {
  35.     printf("%d ", vtest[i]);
  36.   }
  37.   printf("]\n");
  38.  
  39.   // Asking the element to be searched for
  40.   int to_find;
  41.   printf("Insert to find: ");
  42.   scanf("%d", &to_find);
  43.  
  44.   // Calling binary search for the result
  45.   printf("Found: %s\n", binary_search(vtest, LEN, to_find) ? "true" : "false");
  46.   printf("Found rec: %s\n",
  47.          binary_search_rec(vtest, LEN, to_find, 0, LEN) ? "true" : "false");
  48.  
  49.   printf("Sum: %d\n", sum(vtest, LEN));
  50.   printf("Sum rec: %d", sum_rec(vtest, LEN, 0));
  51.   return 0;
  52. }
  53.  
  54. int sum(int v[], size_t length) {
  55.   int s = 0;
  56.   for (size_t i = 0; i < length; i++) {
  57.     s += v[i];
  58.   }
  59.   return s;
  60. }
  61.  
  62. int sum_rec(int v[], size_t length, size_t index) {
  63.   if (length == index)
  64.     return 0;
  65.   return v[index] + sum_rec(v, length, index + 1);
  66. }
  67.  
  68. // Loop implementation
  69. bool binary_search(int v[], size_t length, int to_find) {
  70.   size_t i = 0, j = length;
  71.  
  72.   while (j - i > 1) {
  73.     // (i + j) / 2
  74.     size_t mid = (i + (j - i) / 2);
  75.  
  76.     if (v[mid] < to_find)
  77.       i = mid;
  78.     if (v[mid] > to_find)
  79.       j = mid;
  80.     if (v[mid] == to_find)
  81.       return true;
  82.   }
  83.  
  84.   printf("i: %d, j: %d\n", i, j);
  85.   return v[i] == to_find || v[j] == to_find;
  86. }
  87.  
  88. bool binary_search_rec(int v[], size_t length, int to_find, size_t i,
  89.                        size_t j) {
  90.   if (!(j - i > 1))
  91.     return v[i] == to_find || v[j] == to_find;
  92.  
  93.   // (i + j) / 2
  94.   size_t mid = (i + (j - i) / 2);
  95.  
  96.   if (v[mid] < to_find)
  97.     i = mid;
  98.   if (v[mid] > to_find)
  99.     j = mid;
  100.   if (v[mid] == to_find)
  101.     return true;
  102.  
  103.   return binary_search_rec(v, length, to_find, i, j);
  104. }
  105.  
  106. bool binary_search_rec_2(int v[], size_t length, int to_find, size_t i,
  107.                          size_t j) {
  108.   if (!(j - i > 1))
  109.     return v[i] == to_find || v[j] == to_find;
  110.  
  111.   // (i + j) / 2
  112.   size_t mid = (i + (j - i) / 2);
  113.  
  114.   if (v[mid] < to_find)
  115.     return binary_search_rec(v, length, to_find, mid, j);
  116.   if (v[mid] > to_find)
  117.     return binary_search_rec(v, length, to_find, i, mid);
  118.   if (v[mid] == to_find)
  119.     return true;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement