Skip to content

Commit

Permalink
Implemented interpolation search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
x0lg0n committed Oct 27, 2024
1 parent be2c635 commit 7ccf2d2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Binary file added Java/InterpolationSearch.class
Binary file not shown.
54 changes: 54 additions & 0 deletions Java/InterpolationSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class InterpolationSearch {
/*
* Interpolation Search:
*
* Working: Interpolation search is an algorithm for searching in a sorted array, particularly effective for uniformly
* distributed data. It estimates the likely position of the target value based on its relationship to the
* range of values in the array, rather than splitting the search space in half as in binary search.
*
* Stability: Interpolation search is not stable, as it does not maintain the order of equal elements.
*
* Time Complexity(Best): O(log log n) - Achieved when elements are uniformly distributed.
*
* Time Complexity(Worst): O(n) - Occurs when data is highly skewed or distribution is non-uniform.
*
* Space Complexity: O(1) - Interpolation search operates in-place, requiring no extra space.
*
* Unique Feature: Interpolation search is well-suited for searching in large, uniformly distributed data sets and
* often outperforms binary search under these conditions. However, its efficiency diminishes on
* non-uniformly distributed data.
*/

public static void main(String args[]) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int result = interpolationSearch(arr, x);

if (result == -1)
System.out.println("Element not present in array");

else
System.out.println("Element found at index " + result);

}

public static int interpolationSearch(int[] arr, int x) {
int low = 0, high = (arr.length - 1);

while (low <= high && x >= arr[low] && x <= arr[high]) {
int pos = low + ((x - arr[low]) * (high - low)) / (arr[high] - arr[low]);

if (arr[pos] == x)
return pos;

if (arr[pos] < x)
low = pos + 1;

else
high = pos - 1;

}
return -1;
}

}

0 comments on commit 7ccf2d2

Please sign in to comment.