(1.3.2) Find: Order finder with Sentinel

Source: Internet
Author: User
Simple order lookups have no requirement on the characteristics of the data table, that is, whether there is an incremental decrement feature that basically does not affect the performance of lookups. Basic death is

is to compare elements from one section of the table to the next, and return the element to the corresponding position in the table if found; otherwise, return a meaningless positional label

Knowledge

/* Order lookup, a array, n is the number of array elements to find, key is the keyword to find *
/int sequential_search (int *a, int n, int key)
{
     int i = 0;
     for (; i < n; i++)
     {
          if (a[i] = = key)
               return i;
     }
     return 0;
}
It is worth mentioning that the idea of setting up a lookout, setting the a[0] to a monitoring whistle avoids the need to judge the subscript after each comparison of the elements

The question of cross-border, thus saving time. Of course, a[0] will not be able to store valid elements.

Here is a find implementation that takes an integer array as an example:
    /* Direct order lookup with Sentinel *
/int sequential_search (int a[],int n,int key)  //n number of elements, key is the element to be found
{
    int i=n;
    A[0]=key;   A[0] is the Sentinel while

    (A[i]!=key)  //If there is no key in the array, it will be A[0]=key
        i--;

    return i;     Lookup failed to return 0
}

General Catalogue

List of algorithms for each search and sort

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.