1, sequential search - Search Algorithm

2019/11/02

 

1, a sequential search (search forward from front to back after ||)

Data element definitions:

typedef struct{

    Keytype key; // key field

    InfoType otherinfo; // other domains

}Elemtype;

Sequence table definition:
typedef struct {

    Element * R; // address memory space group

    int length; // current length of

}SSTable;

A sequential search algorithm: [time complexity: O (n)]

int Search_Seq(SSTable ST,KeyType key){

    for(i=ST.length;i>=1;--i)

     if (ST.R [i] .key == key) return i; // looking forward from the rear

    return 0;

}

Algorithm 2 sequentially disposed lookout Find: [time complexity: O (n)]

int Search_Seq(SSTable ST,Keytype key){

    ST.R [0] .key = key; // "sentinel", can be placed at the standard to compete with

    for (i = ST.length; ST.R [i] = key;! --i); // looking forward from the rear

    return i;

}

 

The use of "sentinel", the order can look at the ST.length≥1000, once to find the average time required reduced by almost half.

Sequential search advantages :

Algorithm is simple and requires no table structure, both for sequential structure, but also for the chain structure, regardless of whether the record by Keyword order can be used.

Sequential search Disadvantages:

Average length greater, search efficiency is low, when n is large, not to use   sequential search.

 

Guess you like

Origin www.cnblogs.com/LinQingYang/p/11780665.html