Data Structure and Algorithm Fundamentals (Wang Zhuo) (27) Search in Linear Table (1): Sequential Search (Linear Search)

Table of contents

Basic basic concepts:

keywords:

Lookup table: (dynamic static)

Several operations are often performed on the lookup table:

Find the evaluation criteria of the algorithm:

1. Sequential search (linear search)

Preconditions:

First form: (regular)

Second form:

Third form:

Sequentially find the final minimal algorithm:

Program implementation: 


Basic basic concepts:

Look at this, you don’t need to turn over the PPT


Find:

Based on a given value, determine a data element (or record) in the lookup table whose key is equal to a given value


keywords:

Used to represent the value of a data item of a data element (or record)

Primary key:

A keyword that can uniquely represent a record [example (for example): exam admission ticket number]

Secondary keywords:

Keywords used to identify several records [Example (for example): name is xx, grade is xx points...]


Lookup table: (dynamic static)

A collection of data elements (or records) of the same type. Due to the loose relationship between the data elements in the collection, the lookup table is a convenient structure

Static lookup table:

Lookup table for query and retrieval operations

Dynamic lookup table:

A lookup table for insertion and deletion operations


Several operations are often performed on the lookup table:

  • 1. Query whether a specific data element is in the lookup table
  • 2. Retrieve various attributes of a specific data element
  • 3. If the query result does not exist, insert a data element into the lookup table
  • 4. If the query result exists, delete a data element in the lookup table

Find the evaluation criteria of the algorithm:

The average number of comparisons of keywords, also known as the average lookup length (ASL)


 Return procedure:

1. Sequential search (linear search)


Preconditions:

#include<iostream>
using namespace std;

typedef int KeyType;
//数据元素类型定义
struct ElemType
{
    KeyType key;  //关键字域
    //...           //其他域
};

struct SSTable
    //Sequential Search Table
{
    ElemType* R;  //表基址
    int length;   //表长
};  
SSTable ST;  //定义顺序表ST

First form: (regular)

  • Compare elements front to back
  • As long as the array element pointed to is the same as the target element we want, it will return
int Search_Seq(SSTable ST, KeyType key)
//Seq:顺序
//此查找表为从1开始,0号位置为空,可按需更改
{
    //正序
    for (int i = 1; i <= ST.length; i++)
        if (ST.R[i].key == key)
            return i;
    return 0;

    //倒序
    /*
    for (int i = ST.length; i >= 1 ; i--)
        if (ST.R[i].key == key)
            return i;
    return 0;
    */
}

Second form:

  • start from scratch
  • As long as the array element pointed to is different from the target element we want
  • just (always) keep going backwards than

In reverse order, the traversal order is reversed

int Search_Seq(SSTable ST, KeyType key)
{
    //正序
    int i;
    for (i = 1; ST.R[i].key != key; i++)
    {
        if (i >= ST.length)
            break;
    }
    if (i > 0)
        return i;
    else
        return 0;

    //倒序
    /*
    int i;
    for (i = ST.length; ST.R[i].key != key; i--)
        {
        if (i <= 0)
            break;
    }
    if (i > 0)
        return i;
    else
        return 0;
    */
}

Third form:

int Search_Seq(SSTable ST, KeyType key)
{
    //正序
    int i;
    for (i = 1 ; ST.R[i].key != key && i <= ST.length; i++);
    return i;

    //倒序
    /*
    int i;
    for (i = ST.length; ST.R[i].key != key && i > 0; i--);
    return i;
    */
}

similar:

  • start from scratch
  • As long as the array element pointed to is different from the target element we want and the compared pointer does not exceed the queue
  • just (always) keep going backwards than

The second and third forms of thought are fundamentally no different from the first

It’s just that the second and third forms put the inequality into the next cycle and put it in the judgment statement of the cycle

The for loop does not execute the statement


Sequentially find the final minimal algorithm:

Problems (deficiencies) of the above algorithm:

Each loop requires two comparisons

So how do we improve it so that we only need to do one comparison per loop instead of two   ?

Improved operation:

Store the keyword key to be checked in the table header (sentry/monitoring post), and compare one by one from the back to the front

Method principle:

Eliminate the step of checking whether the search is complete at each step in the search process

And it is guaranteed that the function will have a final return result no matter what

Program implementation: 

int Search_Seq(SSTable ST, KeyType key)
{
    //正序
    ST.R[0].key = key;
    int i;
    for (i = 1; ST.R[i].key != key; i++);
    return i;

    //倒序
    /*
    ST.R[0].key = key;
    int i;
    for (i = ST.length; ST.R[i].key != key; i--);
    return i;
    */
}

When ST.lenath is large, this improvement can almost halve the average time required to do a lookup

Time and space complexity: (not counting sentinels)

Time complexity O(n), space complexity O(1), ASL=(n+1)/2


Additionally, there is a

For details on disputes over the use of [ST.R[ ]], see:

Fundamentals of Data Structure and Algorithm (Wang Zhuo) (26) Search of Linear Tables (2): Sequential Search (Binary Search, Block Search)

Question (1): [ST.R[mid].key]

Guess you like

Origin blog.csdn.net/Zz_zzzzzzz__/article/details/130084277