Data Structures-static lookups

Source: Internet
Author: User


This paper mainly discusses the various implementation methods of sequential table, ordered table, Index table, and hash table lookup, and the average finding length of the corresponding finding method in the equal probability case.

 查找表(Search Table):相同类型的数据元素(对象)组成的集合,每个元素通常由若干数据项构成。   关键字(Key,码):数据元素中某个(或几个)数据项的值,它可以标识一个数据元素。若关键字能唯一标识一个数据元素,则关键字称为主关键字(Primary Key) ;将能标识若干个数据元素的关键字称为次关键字(Secondary Key) 。   查找/检索(Searching):根据给定的K值,在查找表中确定一个关键字等于给定值的记录或数据元素。

There are records in the lookup table that meet the criteria: the search succeeds; results: The record information that is found or the location of the record in the lookup table. There are no records in the
lookup table that meet the criteria: Lookup failed. The
is divided into static lookup tables and dynamic lookup tables based on how the lookup table operates.
Static search: Find table only:
(1) to see whether a particular data element is in a lookup table,
(2) retrieves a specific element and various properties.
Dynamic Lookup table: During the lookup process, you can do the following:
(1) You can insert a data element that does not exist in the lookup table, and
(2) deletes a record that already exists from the lookup table.
in order to improve the efficiency of the search, in addition to the application of a good search algorithm, will also set up a special data structure for the search to store information, such as tables, trees and so on.
Find a table's storage structure:
A lookup table is a very flexible data structure that can be stored in a variety of ways.
Depending on the storage structure, the lookup method can be divided into three main categories:
① sequential table and linked list lookup: Compare the given k value with the keywords recorded in the lookup table, find the record you want to find, and
② hash list: Directly access the lookup table based on the given K value to find the records you want to find;
③ Index Lookup Table lookup: First determine the block in which the record is to be found based on the index, and then find the record you want to find from the block.
Generally, the keywords that are considered to be recorded are types that can be compared, such as Integer, character, real, and so on, the types of keywords, data elements, etc. that are discussed in later sections of this chapter are described as follows:
A typical keyword type description is:

typedef  float   KeyType ;       /*  实型  */typedef  int   KeyType ;       /*  整型  */typedef  char  KeyType ;       /*  字符型  */       数据元素类型的定义是:typedef  struct  RecType{  KeyType  key ;          /* 关键字码  *//* 其他域  */}RecType ;
Static Lookup

ADT static_searchtable{
A data Object D:D is a collection of data elements that have the same attributes.
Each data element has a uniquely identified keyword.
Data Relationship R: The data element belongs to a collection.
Basic Operation P:
} ADT static_searchtable
Linear tables are the simplest way to organize a lookup table, and this section describes some of the main ways to find a linear table.

Sequential Lookup

Application: The elements in the lookup table are not sorted by keyword (i.e. unordered linear table)
1 Finding Ideas
From one end of the table (first or last), the record's keyword is compared with the given k value, and if a record's keyword and a given k value are equal, the lookup succeeds; otherwise, if the complete table is scanned and the corresponding record is not found, the lookup fails. The type of the sequential table is defined as follows:

#define MAX_SIZE  100typedef  struct  SSTable{  RecType  elem[MAX_SIZE] ;    /*  顺序表  */int  length ;     /*  实际元素个数  */}SSTable ;
int  Seq_Search ( SSTable  ST , KeyType key){   int i ;   ST. elem[0].key=key ;    /*  设置监视哨兵,失败返回0  */for (i=ST.length; i>=1;i--)     if (ST. elem[i].key==key)            returnreturn0
Binary find

Prerequisites: All records in the lookup table are ordered by keyword (ascending or descending).
During the lookup process, determine the range of records to be found in the table, and then gradually narrow the range (one half of the unknown origin records each time) until a record is found or cannot be found.
1 Finding Ideas
The lower, upper, and middle position pointers for the interval to be searched are represented by low, high, and mid, and the initial value is low=1,high=n.
⑴ take the middle position mid:mid=? (Low+high)/2? ;
⑵ compares the key of the intermediate position record with the given K value:
① equal: Find success;
② greater than: Unknown origin record in the first half of the interval, modify the upper bound pointer: high=mid-1, turn ⑴;
③ less than: Unknown origin record in the second half of the interval, modify the Nether pointer: low=mid+1, turn ⑴;
Until it crosses the border (Low>high), the lookup fails.

2Algorithm implementationintBin_search (sstable ST, KeyType key) {intlow=1, High=st.length,Mid; while(LowMid= (Low+high)/2;if(ST. elem[Mid].key== key)) return (Mid) ;Else if(ST. elem[Mid].key<key)) low=Mid+1;ElseHigh=Mid-1;} Return0) ; /* Lookup failed */}
Fibonacci Find
Fibonacci Lookup method is based on the characteristics of the Fibonacci sequence to split the lookup table. The Fibonacci sequence is defined as:F(0)=0,F(1)=1,F(j) =F(J-1)+F(J-2) 。 If common -(F(8)-1) Number, j=8, then mid= low+F(7)-1= -If key <a[ -], then low=1, high= ANext: j=7, mid= low+F(6)-1=8If key >a[ -], then low= -, high= -(F(8) -1) Next: j=6, mid=low+F(5)-1= -

1 Finding Ideas
The number of records in the lookup table is 1 smaller than the number of Fibonacci, which is set N=f (j)-1. The lower, upper, and split positions of the interval to be searched are indicated by low, high and mid, and the initial value is low=1,high=n.
⑴ Take Division position mid:mid=low+f (j-1)-1;
⑵ Compare the keyword of the split position record with the given K value:
① equal: Find success;
② greater than: Unknown origin recorded in the first half of the interval (interval length is f (j-1)-1), modified upper bound pointer: high=mid-1, turn ⑴;
③ less than: Unknown origin recorded in the second half of the interval (interval length is f (j-2)-1), modify the lower bound pointer: low=mid+1, turn ⑴;
Until it crosses the border (Low>high), the lookup fails.
Algorithm implementation
In the implementation of the algorithm, in order to avoid frequent calculation of the number of Fibonacci, two variables F1 and F2 can be used to save the current number of adjacent two Fibonacci, so that in the subsequent calculation may be recursively calculated.

int fib(int N){int I,F,F0=0,F1=1;if(N==0)  return(0) ;if(N==1)  return(1) ; for(I=2;I<=N;I++ ){F=f0+f1;F0=F1;F1=F; }return(F) ;}int Fib_search(RecType ST[] ,KeyType Key,int N)/* in ordered tableSTUsedFibonaccimethod to find the keywordKeyRecords of */{int  Low=1,  High,Mid,F1,F2; High=fib (N)-1;F1=fib (n-1);F2=fib (n-2); while( Low<=High ){Mid=low+f1-1;                          if(ST.[Mid]. Key==key)return(Mid) ;Else if(Key<ST.[Mid]. Key) )                                { High=Mid-1;F2=F1-f2;F1=F1-f2; }Else{ Low=mid+1;F1=F1-f2;F2=f2-f1; }                      }return(0) ; }

Fibonacci Find time Complexity also O (LOGN)
Average performance, better than binary lookups, but worst-case performance is worse than binary lookups.
Binary Lookup requires addition and division operations, but Fibonacci lookup requires only adding and subtraction operations.

Block Lookup

Block Lookup (Blocking search), also known as index order lookup, is the synthesis of the previous two search methods.
1 organization of the lookup table
① divides the lookup table into several pieces. The Inter-block order, that is, all the record keywords of block i+1 are greater than (or less than) block I record keywords; the block is unordered.
② attaches an index table based on the lookup table, the index table is ordered by the keyword, and the composition of the records in the index table is

The block index takes into account that the subdivision blocks do not need to be ordered, which greatly increases the overall search speed and is widely used in the database table lookup technology.

Algorithm implementationtypedef structindextype{KeyType Maxkey;/ * The largest keyword in the block * /   intstartpos;/ * Start position of block * /}index;intBlock_search (RecType st[], Index ind[], KeyType key,intNintb/ * Find the record for key in the chunked index Table * /         / * Table length is n, block number is b * /{intI=0, J, K; while((i<b) &&lt (Ind[i].maxkey, key)) i++;if(I&GT;B) {printf("\nnot found");return(0); }j=ind[i].startpos; while((j<n) &AMP;&AMP;LQ (St[j].key, Ind[i].maxkey)) {if(EQ (St[j].key, key)) Break; j + +;}/ * Find inside block * /           if(j>n| |! EQ (St[j].key, key)) {j=0;printf("\nnot found"); }return(j); }

Data Structures-static lookups

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: 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.