Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Quadratic Search #1448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions searching/quadratic_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @file
* @brief Program to perform a "quadratic search" of a target
* value in a given *sorted* array.
* @authors [Your Name] - recursive and iterative algorithms
*/

#include <assert.h>
#include <stdio.h>

/** Recursive implementation
* \param[in] arr array to search
* \param[in] l left index of search range
* \param[in] r right index of search range
* \param[in] x target value to search for
* \returns location of x assuming array arr[l..r] is present
* \returns -1 otherwise
*/
int quadratic_search_recursive(const int *arr, int l, int r, int x)
{
if (r >= l)
{
// Calculate "midpoint" using a quadratic function
int mid = l + ((r - l) * (r - l)) / ((r - l + 1) * (r - l + 1));

// Check if element is present at the midpoint
if (arr[mid] == x)
return mid;

// If the element is smaller than arr[mid], search in the left subarray
if (arr[mid] > x)
return quadratic_search_recursive(arr, l, mid - 1, x);

// Otherwise, search in the right subarray
return quadratic_search_recursive(arr, mid + 1, r, x);
}

// Element is not present in the array
return -1;
}

/** Iterative implementation
* \param[in] arr array to search
* \param[in] l left index of search range
* \param[in] r right index of search range
* \param[in] x target value to search for
* \returns location of x assuming array arr[l..r] is present
* \returns -1 otherwise
*/
int quadratic_search_iterative(const int *arr, int l, int r, int x)
{
while (l <= r)
{
// Calculate "midpoint" using a quadratic function
int mid = l + ((r - l) * (r - l)) / ((r - l + 1) * (r - l + 1));

// Check if element is present at the midpoint
if (arr[mid] == x)
return mid;

// If the element is smaller than arr[mid], search in the left subarray
if (arr[mid] > x)
r = mid - 1;
else // Otherwise, search in the right subarray
l = mid + 1;
}

// Element is not present in the array
return -1;
}

/** Test implementations */
void test()
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Test 1.... ");
int x = 10;
int result = quadratic_search_recursive(arr, 0, n - 1, x);
assert(result == 3);
printf("passed recursive... ");
result = quadratic_search_iterative(arr, 0, n - 1, x);
assert(result == 3);
printf("passed iterative...\n");

printf("Test 2.... ");
x = 5;
result = quadratic_search_recursive(arr, 0, n - 1, x);
assert(result == -1);
printf("passed recursive... ");
result = quadratic_search_iterative(arr, 0, n - 1, x);
assert(result == -1);
printf("passed iterative...\n");
}

/** Main function */
int main(void)
{
test();
return 0;
}
Loading