Ordered table Lookup
/* Main function */public class Ordertablesearch {public static void main (string[] args) {int [] a= {0,1,16,24,35,47,59,62, 73,88,99}; System.out.println (Fibonaccisearch (A, 10, 88)); System.out.println (Insertkeysearch (A, 10, 88)); System.out.println (BinarySearch (A, 10, 88));}
First, binary search
/* binary Find *//* output: 9 */static int BinarySearch (int [] A, int n, int key) {int low, high, Mid;low = 0;high = N;while (Low &L T;= high) {mid = (low + high)/2;/* binary */if (Key < A[mid]) {high = mid-1;} else if (key > A[mid]) {low = mid + 1;} else return mid;} return 0;}
Second, interpolation search
/* Interpolation sort *//* output: 9 */static int Insertkeysearch (int [] A, int n, int key) {int low, high, Mid;low = 0;high = N;while (Low < = high) {/* interpolation lookup calculation Formula */mid = Low + (high-low) * (Key-a[low])/(A[high]-a[low]), if (Key < A[mid]) {high = mid-1;} else if (key > A[mid]) {low = mid + 1;} else return mid;} return 0;}
Third, Fibonacci find
/* Fibonacci sort *//* Output: 9 */static int Fibonaccisearch (int [] A, int n, int key) {int [] F = {0,1,1,2,3,5,8,13,21,34};int low, HI GH, Mid, I, K;low = 1;high = N;k = 0;while (n > F[k]-1)/* Calculates the position of N in the Fibonacci sequence */k++;while (Low <= high) {mid = low + F[k -1] -1;if (Key < A[mid]) {high = Mid-1;k = K-1;} else if (key > A[mid]) {low = mid + 1;k = k-2;} else {if (Mid <= N) return Mid;elsereturn n;}} return 0;}
Comparison of four or three ways to find
Average performance: Fibonacci > Binary > interpolation, because binary lookup is an operation of addition and division, with interpolation of arithmetic, Fibonacci plus minus operations.
(Java) ordered table lookup-binary lookup, interpolation lookup, Fibonacci lookup