Find in-Line table lookup and ordered table lookup (including binary search, interpolation lookup, Fibonacci Lookup) is relatively simple, direct code, the code contains detailed comments.
1#include <iostream>2 using namespacestd;3 4 //Sequential table lookup (linear lookup, static table lookup) time complexity O (n)5 intSeq_search (int*s,intNintkey)6 {7s[0] = key;//a Sentinel is set up to avoid having to determine if the search location is out of bounds after each comparison8 inti =N;9 while(S[i]! =key)Ten { One--i; A } - returni; - } the - //binary lookup (binary lookup) Prerequisites: The recorded key code must be ordered, must be sequential storage time complexity O (logn) - intBinary_search (int*s,intNintkey) - { + intLow,mid,high; -Low =1; +High =N; A while(Low <=High ) at { -Mid = (low + high)/2; - if(S[mid] >key) - { -High = mid-1; -}Else if(S[mid] <key) in { -Low = mid +1; to}Else + returnmid; - } the return 0; * } $ Panax Notoginseng //Interpolation lookup (the average performance of the interpolation lookup algorithm is higher than the binary lookup algorithm for a lookup table with a larger table size and a more uniform keyword distribution) - intB_search (int*s,intNintkey) the { + intLow,mid,high; ALow =1; theHigh =N; + while(Low <=High ) - { $MID = low + (high-low) * (Key-s[low])/(S[high]-S[low]); $ if(S[mid] >key) - { -High = mid-1; the}Else if(S[mid] <key) - {WuyiLow = mid +1; the}Else - returnmid; Wu } - return 0; About } $ - //calculate the Fibonacci sequence - voidFibonacci (int*F) - { Af[0]=0; +f[1]=1; the for(inti =2;i<Ten; i++) - { $F[i] = f[i-1] + f[i-2]; the } the } the the //Fibonacci Lookup (time complexity is O (logn)) - intFibonacci_search (int*s,intNintKeyint*F) in { the intlow,high,mid,i,k; theLow =1; AboutHigh =N; theK =0;//K is the subscript of the Fibonacci sequence. the while(n > F[k]-1)//calculates the position of N in the Fibonacci sequence the { +k++; - } the for(i = N;i < F[k]-1; i++)//to complement a dissatisfied valueBayi { theS[i] =S[n]; the } - //Start Finding - while(Low <=High ) the { theMID = low + f[k]-1; the if(Key <S[mid]) the { -High = mid-1; theK = k-1;//Fibonacci sequence subscript minus one bit . the}Else if(Key >S[mid]) the {94Low = mid +1; theK = k-2;//Fibonacci sequence subscript minus two bits the}Else the {98 if(Mid <=N) About { - returnmid;101}Else102 returnN//if mid>n, the description is the value of the complement, return n103 }104 } the return 0;106 }107 108 voidMain ()109 { the intS[] = {0,1, -, -, *, -, -, +, the, the, About};111 intn = One; the intM,a;113 intfbi[Ten]; the Fibonacci (FBI); the thecout <<"Enter the order table to find the number you want to find:";117CIN >>m;118A =Seq_search (s,n,m);119cout <<"Sequential Table Lookup"<< m <<"the location in:"<< a << Endl <<Endl; - 121cout <<"enter two points to find the number you want to find:";122CIN >>m;123A =Binary_search (s,n,m);124cout <<"Two-point search"<< m <<"the location in:"<< a << Endl <<Endl; the 126cout <<"Enter the interpolation value to find the number you want to find:";127CIN >>m; -A =B_search (s,n,m);129cout <<"Interpolation Lookup"<< m <<"the location in:"<< a << Endl <<Endl; the 131cout <<"enter Fibonacci to find the number you want to find:"; theCIN >>m;133A =Fibonacci_search (S,N,M,FBI);134cout <<"Fibonacci Find"<< m <<"the location in:"<< a << Endl <<Endl;135 136System"Pause");137}
Operation Result:
Sequential table lookups and ordered table lookups