Skip to content

Commit d13460d

Browse files
committed
simplify formula
1 parent cfa2d57 commit d13460d

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

java2/dsa/search/examples/InterpolationSearch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
public class InterpolationSearch {
66

77
public static void main(String[] args) {
8-
int target = 10_000_000;
9-
int[] searchRoom = DataSample.getSortedNumbersFromOneTill(100_000);
8+
int target = 10;
9+
int[] searchRoom = DataSample.getSortedNumbersFromOneTill(100);
1010
System.out.println(InterpolationSearch.search(target, searchRoom));
1111
}
1212

@@ -17,7 +17,7 @@ public static int search(int target, int[] searchRoom) {
1717
// InterpolationSearch.showSearchStatus(low, high, searchRoom);
1818
int lowValue = searchRoom[low];
1919
int highValue = searchRoom[high];
20-
int middleIndex = low + ((target - lowValue) / (highValue - lowValue)) * (high - low);
20+
int middleIndex = low + (high - low) * (target - lowValue) / (highValue - lowValue);
2121
int middleValue = searchRoom[middleIndex];
2222
if (middleValue == target) {
2323
return middleIndex;

0 commit comments

Comments
 (0)