Skip to content
Merged
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
23 changes: 14 additions & 9 deletions Searching-algorithms/Interpolationsearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ public static void main(String[] args) {
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
}
public static int interpolationSearch(int arr[], int lo, int hi, int x) {
int pos;
if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
pos = lo + ((hi - lo) * (x - arr[lo])) / (arr[hi] - arr[lo]);
if (arr[pos] == x)
return pos;
if (arr[pos] < x)
return interpolationSearch(arr, pos + 1, hi, x);
}
public static int interpolationSearch(int arr[], int lo, int hi, int x) {
int pos;
if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
if (arr[lo] == arr[hi]) {
if (arr[lo] == x)
return lo;
return -1;
}
pos = lo + ((hi - lo) * (x - arr[lo])) / (arr[hi] - arr[lo]);
if (arr[pos] == x)
return pos;
if (arr[pos] < x)
return interpolationSearch(arr, pos + 1, hi, x);
if (arr[pos] > x)
return interpolationSearch(arr, lo, pos - 1, x);
}
Expand Down
Loading