|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Program to perform a "quadratic search" of a target |
| 4 | + * value in a given *sorted* array. |
| 5 | + * @authors [Your Name] - recursive and iterative algorithms |
| 6 | + */ |
| 7 | + |
| 8 | +#include <assert.h> |
| 9 | +#include <stdio.h> |
| 10 | + |
| 11 | +/** Recursive implementation |
| 12 | + * \param[in] arr array to search |
| 13 | + * \param[in] l left index of search range |
| 14 | + * \param[in] r right index of search range |
| 15 | + * \param[in] x target value to search for |
| 16 | + * \returns location of x assuming array arr[l..r] is present |
| 17 | + * \returns -1 otherwise |
| 18 | + */ |
| 19 | +int quadratic_search_recursive(const int *arr, int l, int r, int x) |
| 20 | +{ |
| 21 | + if (r >= l) |
| 22 | + { |
| 23 | + // Calculate "midpoint" using a quadratic function |
| 24 | + int mid = l + ((r - l) * (r - l)) / ((r - l + 1) * (r - l + 1)); |
| 25 | + |
| 26 | + // Check if element is present at the midpoint |
| 27 | + if (arr[mid] == x) |
| 28 | + return mid; |
| 29 | + |
| 30 | + // If the element is smaller than arr[mid], search in the left subarray |
| 31 | + if (arr[mid] > x) |
| 32 | + return quadratic_search_recursive(arr, l, mid - 1, x); |
| 33 | + |
| 34 | + // Otherwise, search in the right subarray |
| 35 | + return quadratic_search_recursive(arr, mid + 1, r, x); |
| 36 | + } |
| 37 | + |
| 38 | + // Element is not present in the array |
| 39 | + return -1; |
| 40 | +} |
| 41 | + |
| 42 | +/** Iterative implementation |
| 43 | + * \param[in] arr array to search |
| 44 | + * \param[in] l left index of search range |
| 45 | + * \param[in] r right index of search range |
| 46 | + * \param[in] x target value to search for |
| 47 | + * \returns location of x assuming array arr[l..r] is present |
| 48 | + * \returns -1 otherwise |
| 49 | + */ |
| 50 | +int quadratic_search_iterative(const int *arr, int l, int r, int x) |
| 51 | +{ |
| 52 | + while (l <= r) |
| 53 | + { |
| 54 | + // Calculate "midpoint" using a quadratic function |
| 55 | + int mid = l + ((r - l) * (r - l)) / ((r - l + 1) * (r - l + 1)); |
| 56 | + |
| 57 | + // Check if element is present at the midpoint |
| 58 | + if (arr[mid] == x) |
| 59 | + return mid; |
| 60 | + |
| 61 | + // If the element is smaller than arr[mid], search in the left subarray |
| 62 | + if (arr[mid] > x) |
| 63 | + r = mid - 1; |
| 64 | + else // Otherwise, search in the right subarray |
| 65 | + l = mid + 1; |
| 66 | + } |
| 67 | + |
| 68 | + // Element is not present in the array |
| 69 | + return -1; |
| 70 | +} |
| 71 | + |
| 72 | +/** Test implementations */ |
| 73 | +void test() |
| 74 | +{ |
| 75 | + int arr[] = {2, 3, 4, 10, 40}; |
| 76 | + int n = sizeof(arr) / sizeof(arr[0]); |
| 77 | + |
| 78 | + printf("Test 1.... "); |
| 79 | + int x = 10; |
| 80 | + int result = quadratic_search_recursive(arr, 0, n - 1, x); |
| 81 | + assert(result == 3); |
| 82 | + printf("passed recursive... "); |
| 83 | + result = quadratic_search_iterative(arr, 0, n - 1, x); |
| 84 | + assert(result == 3); |
| 85 | + printf("passed iterative...\n"); |
| 86 | + |
| 87 | + printf("Test 2.... "); |
| 88 | + x = 5; |
| 89 | + result = quadratic_search_recursive(arr, 0, n - 1, x); |
| 90 | + assert(result == -1); |
| 91 | + printf("passed recursive... "); |
| 92 | + result = quadratic_search_iterative(arr, 0, n - 1, x); |
| 93 | + assert(result == -1); |
| 94 | + printf("passed iterative...\n"); |
| 95 | +} |
| 96 | + |
| 97 | +/** Main function */ |
| 98 | +int main(void) |
| 99 | +{ |
| 100 | + test(); |
| 101 | + return 0; |
| 102 | +} |
0 commit comments