Data structure------------lookup (based on the sorting)

Source: Internet
Author: User

-----------------------article refer to "Big talk data Structure"--------------------------

1. Basic Concepts

Lookup tables: Collections that are composed of elements of the same type

Keyword: The value of a data item in a data element

Static lookup tables and dynamic lookup tables

Static lookup table: query whether a "specific" data element is in a lookup table

Querying for a "specific" data element and various properties

Dynamic lookup Table: Inserts a data element that does not exist in the lookup table during a lookup, or deletes a data element that already exists from the lookup table

2. Comparison of various search algorithms

(1) Linear search

Start with the first (or last) record in the table. A comparison of the keywords that are recorded and the given ratios, if the keyword of a record is equal to the given value, the lookup succeeds.

C Language Code

1 int sequential_serach (int a[],int n,int key) {   //a arrays, n is the length of the array to find, key is the keyword to find 2   int i;     3 for   (i=1;i<=n;i++) {4  5 if (a[i]=key) 6   return i; 7} 8 return 0; 9}

Java code

1 public static int Sequuential_search (int. [] A,int key) {2 for     (int i=1;i<=a.length;i++) {3           if (a[i]==key) {4                  return i;5             }           6     }7     return 0;8}
1///Linear lookup Table Optimization 2 public int sequential_search2 (int [] A,int key) {3    int i=a.length;4    a[0]=key;    Set a[0] to the keyword value 5    while (a[i]!=key) {    //From the end of the array to find 6       i--; 7       }8      return i;   Returns 0 This indicates that the lookup failed, a[1]-a[n] without the keyword 9}

(2) Binary search

This method is based on the premise that the records in the linear table must be sequential, that is, the linear table must be stored sequentially, the basic idea: to take the intermediate record as the comparison object, if the given value and the middle record of the same keyword, which is still found successful, if the given value is less than the middle record of the keyword, then the left half Instead, look in the right half of the area and repeat it until you find it.

C Language Code

1 int Binary_serach (int a [],int n,int key) {2      int low,hing,mid; 3      low=1; 4      high=n; 5 while      (Low<=high) {6        mid= (Low+high)/2; 7        if (Key<a[mid]) 8              high=mid-1; 9        else if (Key>a[mid])              low=mid-1;        else12             return mid;13       }14       return 0;15}

Java code

1 public int Binary_search (int [] a,int n,int key) {2      int low,high,mid; 3      low=1; 4      High=
      
       n; 5 while      (low<= high
       ) {6        mid= (Low+high)/2; 7 if (key<A[mid]) 8 high=mid-1; 9 Else if ( Key>A[mid]) low=mid-1else12 return mid;13 }14 return 0; /c27> 
      

(3) Interpolation search

Go directly to the code.

On the basis of the binary search, mid= (Low+high)/2 is replaced with the following
mid=low+ (high-low) * (Key-a[low])/(A[high]-a[low])

1 public int Binary_search (int [] a,int n,int key) {2 int low,high,mid; 3 low=1; 4 high=N; 5 while (low<= high) {6 mid=low+ (high-low) * (Key-a[low])/(a[high]-A[low]); 7 if (key<A[mid]) 8 High=mid-1; 9 else if (key>a[mid]) low=mid-1; else12 return mid;13}14 return 0; 15}

(4) Fibonacci Search

Let's talk about the golden divide.

The Golden section refers to dividing the whole into half, and the ratio of the majority to the whole part equals to the ratio of the smaller part to the larger one, the ratio of which is about 0.618. This ratio is recognized as the most aesthetic-induced proportion and is therefore called the Golden Section.

Suppose a Fibonacci sequence is 1,1,2,3,5,8,13,21,34,55,89,

As the number increases, the ratio of the sum of a number of Fibonacci numbers to the number of the latter is getting closer to 0.618, and we are using this property to find it, a simple one as shown below

How is the start position of the mid determined?

When we first split up to meet the golden ratio split, assuming that the number of arrays to be looked for N, we found in the Fibonacci sequence to meet F[k] greater than N, and the closest to N, get F[k] after the first two values as the basis for the division (F[k-1] and f[k-2] two parts.

Most of the instructions omit a description of the condition: n=f (k)-1, the number of records in the table is 1 smaller than one Fibonacci number. What is this for?

is for the unification of the format, in order to facilitate the writing of recursive or cyclic programs. The data in the table is f (k)-1, split with the mid value and one for the other, then F (k)-2 is left. Just give two sub-sequences, the number of each subsequence is F (k-1)-1 and F (k-2)-1, the format is consistent with the previous one. Otherwise, the number of elements per subsequence may be f (k-1), F (k-1) -1,f (K-2), F (k-2)-1, and writing a program can be cumbersome.

Can be divided into three situations when looking for

(1) When equal, the mid position is the position to find

(2) When the key to be found is less than the value of the mid position, that is, on the left side of the diagram, update the high value,high=mid-1,k-=1; Description: Low=mid+1 indicates that the element to be found is within the [low,mid-1] range, the number of elements in the k-=1 description range [Low,mid-1] is F (k-1)-1, so you can recursively apply Fibonacci lookups

(3) When the key to be searched is greater than the value of the mid position, that is, on the right side of the diagram, the value of low is updated,low=mid+1,k-=2; Description: Low=mid+1 indicates that the element to be found is within the [mid+1,hign] range, the number of elements in the k-=2 description range [Mid+1,high] is n (F (k-1)) = Fk-1-f (k-1) =fk-f (k-1) -1=f (k-2)-1, So it's possible to recursively apply Fibonacci lookups

The C language code is as follows

1 //Fibonacci Lookup2 intFibonacci_search (intA[],intNintkey) {3     intlow,high,mid,i,k;4low=1;5High=N;6k=0;7      while(n>f[k]-1){//calculates the position of n at the Fibonacci sequence8k++;9     }Ten      for(i=n;i<f[k]-1; i++) {//to complement a dissatisfied value Onea[i]=A[n]; A     } -      while(low<=High ) { -mid=low+f[k]-1;//calculates the subscript for the current split the         if(key<A[mid]) { -high=mid-1; -k=k-1; -         } +         Else if(key>A[mid]) { -Low=mid+1; +k=k-2; A         } at         Else{ -             if(mid<=N) -                 returnMid//If the equality indicates that mid is the value found -             Else -                 returnN//if the value greater than n is complete, return n -         } in  -     } to     return 0;//didn't find +}

Data structure------------lookup (based on the sorting)

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.