mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
feat: Reworked interpolation_search.cpp (#1855)
* feat: Reworked interpolation_search.cpp * Update interpolation_search.cpp * clang-format and clang-tidy fixes forc98405b3
* Update search/interpolation_search.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/interpolation_search.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/interpolation_search.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/interpolation_search.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes ford4eac660
* Update search/interpolation_search.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/interpolation_search.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * fix: apply suggestions from code review Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
fe692e8e01
commit
19f48ae202
@ -1,61 +1,123 @@
|
|||||||
/**
|
|
||||||
* \file
|
|
||||||
* \brief [Interpolation
|
|
||||||
* search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm
|
|
||||||
*/
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
/** function to search the value in an array using interpolation search
|
/******************************************************************************
|
||||||
* \param [in] arr array to search in
|
* @file
|
||||||
* \param [in] value value to search for
|
* @brief [Interpolation search
|
||||||
* \param [in] len length of array
|
* algorithm](https://en.wikipedia.org/wiki/interpolation_search)
|
||||||
* \returns index where the value is found
|
*
|
||||||
* \returns 0 if not found
|
* @details
|
||||||
*/
|
* interpolation search resembles the method by which people search a telephone
|
||||||
int interpolation_search(int arr[], int value, int len) {
|
* directory for a name (the key value by which the book's entries are ordered):
|
||||||
int low = 0, high, mid;
|
* in each step the algorithm calculates where in the remaining search space
|
||||||
high = len - 1;
|
* the sought item might be, based on the key values at the bounds of the search
|
||||||
|
* space and the value of the sought key, usually via a linear interpolation.
|
||||||
|
* The key value actually found at this estimated position is then compared to
|
||||||
|
* the key value being sought. If it is not equal, then depending on the
|
||||||
|
* comparison, the remaining search space is reduced to the part before or
|
||||||
|
* after the estimated position. This method will only work if calculations
|
||||||
|
* on the size of differences between key values are sensible.
|
||||||
|
|
||||||
while (arr[low] <= value && arr[high] >= value) {
|
* ### Complexities
|
||||||
mid = (low +
|
*
|
||||||
((value - arr[low]) * (high - low)) / (arr[high] - arr[low]));
|
* //n is the number of element in the array.
|
||||||
if (arr[mid] > value)
|
*
|
||||||
high = mid - 1;
|
* Worst-case time complexity O(n) (when items are distributed
|
||||||
else if (arr[mid] < value)
|
exponentially)
|
||||||
low = mid + 1;
|
* Average time complexity O(log2(log2 n))
|
||||||
else
|
* space complexity 0(1)
|
||||||
return mid;
|
*
|
||||||
|
* @author [Lajat Manekar](https://github.com/Lazeeez)
|
||||||
|
* @author Unknown author
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <algorithm> /// for std::sort function
|
||||||
|
#include <cassert> /// for std::assert
|
||||||
|
#include <iostream> /// for IO operations
|
||||||
|
#include <vector> /// for std::vector
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* @namespace search
|
||||||
|
* @brief Searching algorithms
|
||||||
|
*******************************************************************************/
|
||||||
|
namespace search {
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* @namespace interpolation_search
|
||||||
|
* @brief Functions for the [Interpolation
|
||||||
|
*Search](https://en.wikipedia.org/wiki/interpolation_search) algorithm
|
||||||
|
*implementation
|
||||||
|
*******************************************************************************/
|
||||||
|
namespace interpolation_search {
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* @brief The main function which implements interpolation search
|
||||||
|
* @param arr vector to be searched in
|
||||||
|
* @param number value to be searched
|
||||||
|
* @returns integer index of `number` in vector `arr`
|
||||||
|
*******************************************************************************/
|
||||||
|
uint64_t interpolationSearch(const std::vector<uint64_t> &arr,
|
||||||
|
uint64_t number) {
|
||||||
|
uint64_t size = arr.size();
|
||||||
|
uint64_t low = 0, high = (size - 1);
|
||||||
|
|
||||||
|
// Since vector is sorted, an element present in array must be in range
|
||||||
|
// defined by corner
|
||||||
|
while (low <= high && number >= arr[low] && number <= arr[high]) {
|
||||||
|
if (low == high) {
|
||||||
|
if (arr[low] == number) {
|
||||||
|
return low;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// Probing the position with keeping uniform distribution in mind.
|
||||||
|
uint64_t pos =
|
||||||
|
low +
|
||||||
|
((static_cast<uint64_t>(high - low) / (arr[high] - arr[low])) *
|
||||||
|
(number - arr[low]));
|
||||||
|
|
||||||
|
if (arr[pos] == number) {
|
||||||
|
return pos; // Condition of target found
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arr[low] == value)
|
if (arr[pos] < number) {
|
||||||
return low;
|
low = pos + 1; // If x is larger, x is in upper part
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
high = pos - 1; // If x is smaller, x is in the lower part
|
||||||
|
}
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** main function */
|
} // namespace interpolation_search
|
||||||
|
|
||||||
|
} // namespace search
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* @brief Self-test implementation
|
||||||
|
* @returns void
|
||||||
|
*******************************************************************************/
|
||||||
|
static void tests() {
|
||||||
|
// testcase
|
||||||
|
// array = [10, 12, 13, 16, 18, 19, 20, 21, 1, 2, 3, 4, 22, 23, 24, 33, 35,
|
||||||
|
// 42, 47] , Value = 33 should return 15
|
||||||
|
std::vector<uint64_t> arr = {{10, 12, 13, 16, 18, 19, 20, 21, 1, 2, 3, 4,
|
||||||
|
22, 23, 24, 33, 35, 42, 47}};
|
||||||
|
sort(arr.begin(), arr.end());
|
||||||
|
uint64_t number = 33; // Element to be searched
|
||||||
|
uint64_t expected_answer = 15;
|
||||||
|
uint64_t derived_answer =
|
||||||
|
search::interpolation_search::interpolationSearch(arr, number);
|
||||||
|
std::cout << "Testcase: ";
|
||||||
|
assert(derived_answer == expected_answer);
|
||||||
|
std::cout << "Passed!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* @brief Main function
|
||||||
|
* @returns 0 on exit
|
||||||
|
*******************************************************************************/
|
||||||
int main() {
|
int main() {
|
||||||
int n, value, re;
|
tests(); // run self-test implementations
|
||||||
|
|
||||||
std::cout << "Enter the size of array(less than 100) : ";
|
|
||||||
std::cin >> n;
|
|
||||||
|
|
||||||
int *array = new int[n];
|
|
||||||
|
|
||||||
std::cout << "array in ascending (increasing) order : " << std::endl;
|
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) std::cin >> array[i];
|
|
||||||
|
|
||||||
std::cout << "Enter the value you want to search : ";
|
|
||||||
std::cin >> value;
|
|
||||||
|
|
||||||
re = interpolation_search(array, value, n);
|
|
||||||
|
|
||||||
if (re == -1)
|
|
||||||
std::cout << "Entered value is not in the array" << std::endl;
|
|
||||||
else
|
|
||||||
std::cout << "The value is at the position " << re << std::endl;
|
|
||||||
|
|
||||||
delete[] array;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user