diff --git a/search/interpolation_search.cpp b/search/interpolation_search.cpp index a8d094baf..403c6fb3f 100644 --- a/search/interpolation_search.cpp +++ b/search/interpolation_search.cpp @@ -1,7 +1,7 @@ /****************************************************************************** * @file - * @brief [interpolation search + * @brief [interpolation search * algorithm](https://en.wikipedia.org/wiki/interpolation_search) * * @details @@ -20,7 +20,8 @@ * * //n is the number of element in the array. * - * Worst-case time complexity O(n) (when items are distributed exponentially) + * Worst-case time complexity O(n) (when items are distributed + exponentially) * Average time complexity O(log2(log2 n)) * space complexity 0(1) * @@ -38,53 +39,55 @@ * @brief Searching algorithms *******************************************************************************/ namespace search { - + /****************************************************************************** * @namespace interpolation_search * @brief interpolation search searching algorihm *******************************************************************************/ - namespace interpolation_search { - - /****************************************************************************** - * @brief The main function which implements binary 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 &arr, uint64_t number) { - uint64_t size = arr.size(); - uint64_t low = 0, high = (size - 1); +namespace interpolation_search { - // 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(high - low) / - (arr[high] - arr[low])) * - (number - arr[low])); +/****************************************************************************** + * @brief The main function which implements binary 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 &arr, + uint64_t number) { + uint64_t size = arr.size(); + uint64_t low = 0, high = (size - 1); - if (arr[pos] == number) { - return pos; // Condition of target found - } - - if (arr[pos] < number) { - 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 - } + // 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(high - low) / (arr[high] - arr[low])) * + (number - arr[low])); - } // namespace interpolation_search + if (arr[pos] == number) { + return pos; // Condition of target found + } + + if (arr[pos] < number) { + 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; +} + +} // namespace interpolation_search } // namespace search @@ -94,13 +97,15 @@ namespace search { *******************************************************************************/ 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 arr = {{10, 12, 13, 16, 18, 19, 20, 21, 1, 2, 3, 4, 22, 23, 24, 33, 35, 42, 47}}; + // 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 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 number = 33; // Element to be searched uint64_t expected_answer = 15; - uint64_t derived_answer = search::interpolation_search::interpolationSearch(arr, number); + uint64_t derived_answer = + search::interpolation_search::interpolationSearch(arr, number); std::cout << "Testcase : "; assert(derived_answer == expected_answer); std::cout << "Passed!"; @@ -110,8 +115,7 @@ void tests() { * @brief Main function * @returns 0 on exit *******************************************************************************/ -int main() -{ - tests(); // Condition of target found +int main() { + tests(); // Condition of target found return 0; }