插補搜尋
December 11, 2021如果除了順序之外,數列還有其他特性,若能善用那些特性,就有機會比二分搜尋來得快速,例如,若要搜尋的資料分佈平均,有線性趨勢的話,可以使用插補(Interpolation)搜尋。
解法思路
插補搜尋是以資料分佈的近似直線進行比例運算,求出中間的索引並進行資料比對,如果取出的值小於要尋找的值,提高下界,如果取出的值大於要尋找的值,降低下界,如此不斷地減少搜尋的範圍。
例如,若以下 K 是指定要尋找的對象,m 是可能的索引值:
程式實作
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
#define SWAP(x,y) {int t; t = x; x = y; y = t;}
void quickSort(int[], int, int);
int interpolationSearch(int[], int);
int main(void) {
srand(time(NULL));
int number[MAX] = {0};
int i;
for(i = 0; i < MAX; i++) {
number[i] = rand() % 100;
}
quickSort(number, 0, MAX-1);
printf("數列:");
for(i = 0; i < MAX; i++)
printf("%d ", number[i]);
int find;
printf("\n輸入尋找對象:");
scanf("%d", &find);
if((i = interpolationSearch(number, find)) >= 0)
printf("找到數字於索引 %d ", i);
else
printf("\n找不到指定數");
printf("\n");
return 0;
}
int interpolationSearch(int number[], int find) {
int low = 0;
int upper = MAX - 1;
while(low <= upper) {
int mid = (upper-low)*
(find-number[low])/(number[upper]-number[low])
+ low;
if(mid < low || mid > upper)
break;
if(find < number[mid])
upper = mid - 1;
else if(find > number[mid])
low = mid + 1;
else
return mid;
}
return -1;
}
void quickSort(int number[], int left, int right) {
if(left < right) {
int s = number[(left+right)/2];
int i = left - 1;
int j = right + 1;
while(1) {
while(number[++i] < s) ; // 向右找
while(number[--j] > s) ; // 向左找
if(i >= j)
break;
SWAP(number[i], number[j]);
}
quickSort(number, left, i-1); // 對左邊進行遞迴
quickSort(number, j+1, right); // 對右邊進行遞迴
}
}
public class Search {
public static int interpolation(int[] number, int des) {
int low = 0;
int upper = number.length - 1;
while(low <= upper) {
int mid = (upper-low)*(des-number[low])
/(number[upper]-number[low]) + low;
if(mid < low || mid > upper)
break;
if(des < number[mid])
upper = mid - 1;
else if(des > number[mid])
low = mid + 1;
else
return mid;
}
return -1;
}
public static void main(String[] args) {
int[] number = {1, 2, 3, 4, 6, 7, 8};
int find = Search.interpolation(number, 2);
System.out.println(find >= 0 ? "找到數值於索引" + find : "找不到數值");
}
}
def search(number, des):
low = 0
upper = len(number) - 1
while low <= upper:
mid = (upper - low) * (des - number[low]) // (number[upper] - number[low]) + low
if mid < low or mid > upper:
break
if des < number[mid]:
upper = mid - 1
elif des > number[mid]:
low = mid + 1
else:
return mid
return -1
number = [1, 4, 2, 6, 7, 3, 9, 8]
number.sort()
find = search(number, 2)
print("找到數值於索引 " + str(find) if find >= 0 else "找不到數值")
object Search {
def interpolation(number: Array[Int], des: Int) = {
var low = 0
var upper = number.length - 1
var result = -1
var isContinue = true
while(isContinue && low <= upper) {
val mid = (upper-low)* (des-number(low)) /
(number(upper)-number(low)) + low
if(mid < low || mid > upper) isContinue = false
if(des < number(mid))
upper = mid - 1
else if(des > number(mid))
low = mid + 1
else {
result = mid
isContinue = false
}
}
result
}
}
val number = Array(1, 2, 3, 4, 6, 7, 8)
val find = Search.interpolation(number, 3)
println(if(find >= 0) "找到數值於索引 " + find else "找不到數值")
# encoding: UTF-8
def search(number, des)
low = 0
upper = number.length - 1
while low <= upper
mid = (upper - low) * (des - number[low]) /
(number[upper] - number[low]) + low
if mid < low || mid > upper
break
end
if des < number[mid]
upper = mid - 1
elsif des > number[mid]
low = mid + 1
else
return mid
end
end
-1
end
number = [1, 4, 2, 6, 7, 3, 9, 8]
number.sort!
find = search(number, 2)
print find >= 0 ? "找到數值於索引 " + find.to_s : "找不到數值", "\n"
function search(lt, des) {
let low = 0;
let upper = lt.length - 1;
while(low <= upper) {
let mid = (upper - low) * Math.floor((des - lt[low]) / (lt[upper] - lt[low])) + low;
if(mid < low || mid > upper) {
break;
}
if(des < lt[mid]) {
upper = mid - 1;
}
else if(des > lt[mid]) {
low = mid + 1;
}
else {
return mid;
}
}
return -1;
}
let lt = [1, 4, 2, 6, 7, 3, 9, 8];
lt.sort((a, b) => a - b)
console.log(search(lt, 2));
search [] _ = -1
search lt n = _search lt n 0 (length lt - 1)
where
_search lt n low upper =
if low > upper then -1
else
let mid = (upper - low) * (n - (lt !! low)) `div` ((lt !! upper) - (lt !! low)) + low
elem = lt !! mid
in
if elem == n then mid
else
if elem < n then _search lt n (mid + 1) upper
else _search lt n low (mid - 1)
main = print $ search [3, 5, 8, 9, 10, 13, 15] 13