|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Program to perform a "sublinear search" of a target |
| 4 | + * value in a given *sorted* array by skipping elements. |
| 5 | + * @authors [Your Name] - iterative and recursive algorithms |
| 6 | + */ |
| 7 | + |
| 8 | +#include <assert.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <math.h> |
| 11 | + |
| 12 | +/** Recursive implementation |
| 13 | + * \param[in] arr array to search |
| 14 | + * \param[in] l left index of search range |
| 15 | + * \param[in] r right index of search range |
| 16 | + * \param[in] x target value to search for |
| 17 | + * \param[in] step_size the size of steps to skip in each search iteration |
| 18 | + * \returns location of x assuming array arr[l..r] is present |
| 19 | + * \returns -1 otherwise |
| 20 | + */ |
| 21 | +int sublinear_search_recursive(const int *arr, int l, int r, int x, int step_size) |
| 22 | +{ |
| 23 | + if (l > r) { |
| 24 | + return -1; // Element is not present in array |
| 25 | + } |
| 26 | + |
| 27 | + int mid = l + step_size; |
| 28 | + |
| 29 | + // Check if we've found the element |
| 30 | + if (mid > r) { |
| 31 | + return -1; // Exceeded range without finding the element |
| 32 | + } else if (arr[mid] == x) { |
| 33 | + return mid; // Element found |
| 34 | + } else if (arr[mid] > x) { |
| 35 | + // If element at mid is greater than x, search the remaining part from l to mid - 1 |
| 36 | + return sublinear_search_recursive(arr, l, mid - 1, x, 1); |
| 37 | + } else { |
| 38 | + // Else continue with a larger step from mid + step_size to r |
| 39 | + return sublinear_search_recursive(arr, mid + 1, r, x, step_size); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** Iterative implementation |
| 44 | + * \param[in] arr array to search |
| 45 | + * \param[in] n length of the array |
| 46 | + * \param[in] x target value to search for |
| 47 | + * \param[in] step_size the size of steps to skip in each search iteration |
| 48 | + * \returns location of x assuming array arr is present |
| 49 | + * \returns -1 otherwise |
| 50 | + */ |
| 51 | +int sublinear_search_iterative(const int *arr, int n, int x, int step_size) |
| 52 | +{ |
| 53 | + int i = 0; |
| 54 | + |
| 55 | + // Traverse the array in steps of step_size |
| 56 | + while (i < n) { |
| 57 | + // Check if element is found at current position |
| 58 | + if (arr[i] == x) { |
| 59 | + return i; |
| 60 | + } |
| 61 | + // If the current element is greater than x, do a linear search backwards |
| 62 | + if (arr[i] > x) { |
| 63 | + for (int j = i - step_size + 1; j < i; j++) { |
| 64 | + if (j >= 0 && arr[j] == x) { |
| 65 | + return j; |
| 66 | + } |
| 67 | + } |
| 68 | + return -1; // Element not found |
| 69 | + } |
| 70 | + // Move to the next step |
| 71 | + i += step_size; |
| 72 | + } |
| 73 | + |
| 74 | + // Check the last block linearly |
| 75 | + for (int j = i - step_size + 1; j < n; j++) { |
| 76 | + if (arr[j] == x) { |
| 77 | + return j; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return -1; // Element is not present in array |
| 82 | +} |
| 83 | + |
| 84 | +/** Test implementations */ |
| 85 | +void test() |
| 86 | +{ |
| 87 | + int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; |
| 88 | + int n = sizeof(arr) / sizeof(arr[0]); |
| 89 | + int step_size = sqrt(n); // Choosing a step size based on array size |
| 90 | + |
| 91 | + printf("Test 1.... "); |
| 92 | + int x = 7; |
| 93 | + int result = sublinear_search_recursive(arr, 0, n - 1, x, step_size); |
| 94 | + assert(result == 3); |
| 95 | + printf("passed recursive... "); |
| 96 | + result = sublinear_search_iterative(arr, n, x, step_size); |
| 97 | + assert(result == 3); |
| 98 | + printf("passed iterative...\n"); |
| 99 | + |
| 100 | + printf("Test 2.... "); |
| 101 | + x = 8; // Element not in array |
| 102 | + result = sublinear_search_recursive(arr, 0, n - 1, x, step_size); |
| 103 | + assert(result == -1); |
| 104 | + printf("passed recursive... "); |
| 105 | + result = sublinear_search_iterative(arr, n, x, step_size); |
| 106 | + assert(result == -1); |
| 107 | + printf("passed iterative...\n"); |
| 108 | +} |
| 109 | + |
| 110 | +/** Main function */ |
| 111 | +int main(void) |
| 112 | +{ |
| 113 | + test(); |
| 114 | + return 0; |
| 115 | +} |
0 commit comments