Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Java implementation of interpolation search
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrazadin committed Oct 3, 2018
1 parent 1213170 commit 18e286e
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions search/interpolation_search/Java/InterpolationSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.Arrays;

public class InterpolationSearch {

private static final int NOT_FOUND = -1;

static int interpolationSearch(int[] arr, int x) {

// Keep track of the two indexes
int lo = 0;
int hi = (arr.length - 1);

while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {

// Calculate the position to check
int pos = lo + (((hi-lo) /
(arr[hi]-arr[lo]))*(x - arr[lo]));

// The key was found!
if (arr[pos] == x){
return pos;
}

// If x is larger, x is in upper part
if (arr[pos] < x) {
lo = pos + 1;
}

// If x is smaller, x is in the lower part
else {
hi = pos - 1;
}
}

// The key was not found!
return NOT_FOUND;
}

// Driver method
public static void main(String[] args)
{
int key = 18;
int[] test = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47};
int index = interpolationSearch(test, key);
System.out.println("The Sample Array: " + Arrays.toString(test));
System.out.println("The Test Key: " + key);
System.out.println("The Index Of The Key: " + index);
}
}

0 comments on commit 18e286e

Please sign in to comment.