二分查詢,插值查詢,斐波那契查詢

2021-09-01 19:12:33 字數 1748 閱讀 3692

二分查詢,插值查詢,斐波那契查詢:

1.二分查詢:

偽**:

while(left

else if key< arr[(left+index)/2]

then right = (left+right)/2

else

left = (left+right)/2

end

示例**:

public int midsearch(listarr, int searchvalue)

else

}return -1;

}

2.插值查詢:

note:區別在於二分法在計算中值索引時候一直是/2 ,而插值查詢的關鍵在於查詢公式。

思路與二分法一樣,只是計算索引時把mid = (left+right)/2 替換為:

left + (key - arr[left]) * (right - left) / (arr[right] - arr[left]);

示例**:

public int interpolationsearch(listarr, int searchvalue)

else

}return -1;

}

3.斐波那契查詢:

1.建立斐波那契陣列

2.查詢

示例**:

private void f_initonce()}}

public int fibonaccisearch(listarr, int key)

else if (key > arr[mid])

else

}return -1;

}

ut:

[testmethod]

public void midsearch()

;var searcher = new searchstudy();

for (var i = 0; i < arr.count; i++)

var tobesearched = 32;

var ret2 = searcher.midsearch(arr, tobesearched);

assert.areequal(ret2, -1);

}[testmethod]

public void interpolationsearch()

;var searcher = new searchstudy();

for (var i = 0; i < arr.count; i++)

var cannotfind = 32;

var ret2 = searcher.interpolationsearch(arr, cannotfind);

assert.areequal(ret2, -1);

}[testmethod]

public void fibonaccisearch()

;var searcher = new searchstudy();

for (var i = 0; i < arr.count; i++)

var cannotfind = 32;

var ret2 = searcher.fibonaccisearch(arr, cannotfind);

assert.areequal(ret2, -1);

}

二分查詢 插值查詢 斐波那契查詢

在有序表中,取中間記錄作為比較物件,將其與給定值相比較,若其等於給定值,則查詢成功 若其小於給定值,則在中間記錄的右半區繼續查詢 若其大於給定值,則在中間記錄的左半區繼續查詢。不斷重複以上過程,直到查詢成功或無匹配記錄。使用該查詢方法的前提條件在於,線性表中的資料必須有序,且必須採用順序儲存的方式。...

順序查詢 二分查詢,斐波那契查詢,插值查詢

include include include using namespace std void printerr else if arr mid ele else return 1 void print int i else if len 2 else if len 3 else if len 3...

有序表查詢(二分查詢,插值查詢,斐波那契查詢)

1.二分查詢 二分查詢非遞迴 int binary search1 int num,int len,int target return 1 二分查詢遞迴 int binary search2 int num,int low,int hight,int target return 1 2.插值查詢 插...