This repository has been archived by the owner on Oct 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java implementation of interpolation search
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |