(Java) Ordered table lookup--binary lookup, interpolation lookup, Fibonacci lookup

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.