A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/df/d39/interpolation__search2_8cpp.html below:

TheAlgorithms/C++: search/interpolation_search2.cpp File Reference

Loading...

Searching...

No Matches

Interpolation search algorithm More...

#include <iostream>

Go to the source code of this file.

◆ InterpolationSearch() int InterpolationSearch ( int A[], int n, int x )

function to search the value in an array using interpolation search

Parameters
[in] arr array to search in [in] value value to search for [in] len length of array
Returns
index where the value is found
-1 if not found

Definition at line 15 of file interpolation_search2.cpp.

15 {

16 int low = 0;

17 int high = n - 1;

18 while (low <= high) {

19 int mid = low + (((high - 1) * (x - A[low])) / (A[high] - A[low]));

20 if (x == A[mid])

21 return mid;

22 else if (x < A[mid])

23 high = mid - 1;

24 else

25 low = mid + 1;

26 }

27

28 return -1;

29}

◆ main()

main function

< passed array A inside the InterpolationSearch function

Definition at line 32 of file interpolation_search2.cpp.

32 {

33 int A[] = {2, 4, 5, 7, 13, 14, 15, 23};

34 int x = 17;

35

38 if (index < 0)

39 std::cout << "Number " << x << " not found" << std::endl;

40 else

41 std::cout << "Number " << x << " is at " << index << std::endl;

42}

int InterpolationSearch(int A[], int n, int x)


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4