Algorithm exercise--sequential lookup

Source: Internet
Author: User

Background:

  I was trying to figure out something else and started using xdebug+webgrind two days ago. The feeling is very convenient, but still just a beginner, many features have not yet been understood. Without the direction to do something, then continue to lay the groundwork, today began to look to find this piece.

Business:

  Sequential lookup-A sequential lookup is the exact location of the same number as a given keyword in a known no (or ordered) Order queue. The principle is that the keyword is compared to the number in the queue from the last one, until it finds the same number as the given keyword, and its disadvantage is inefficiency.

The concept is very clear, the shortcomings are easy to see, the direct sticker code (I am here as an example of the order table).

  

<?PHP/** * Linear table Node class * is used to store the values of a number set and to implement sequential lookups of linear tables **/classrectype{/** * Store numeric values * * @var int*/     Public $val; /** * Store additional Information * * @var mix*/     Public $other;  Public function__construct ($val,$other=NULL)    {        $this->val =$val; $this->other =$other; }}/** Search for the position of a keyword in a given number of strings * * @param array of given number of arrays * @param int Key number to find * * return int return position if not found return false*/functionSeq_search ($search _val,$search _key){    $r=Array(); Init_rectype ($r,$search _val,$search _key); $num=Count($r);  while($r[$num--]->val! =$search _key); if($num<0)    {        return false; }    Else    {        return $num+1; }}/** * establish linear table of sequential structure * * @param array of linear table arrays container * @param array linear table cell values collection * @param int key number to find * * return*/functionInit_rectype (&$r,$search _val,$search _key){    $r[0] =NewRecType$search _key); if(Is_array($search _val))    {        foreach($search _val  as $val)        {            $r[] =NewRecType$val); }    }    Else    {        Exit(' Unrecognized search type '); }    return ;}//Test$search=Array(5,3,48,646,489,123,465468,12,54,6,2);$search _key= 12;$result= Seq_search ($search,$search _key);if($result!==false){    Echo $search _key, ' in the sequence of the first ',$result, ' bit ';
12 in the 8th bit of the sequence}Else{ Echo $search _key, ' does not exist in the sequence ';}?>

There is also a lookout for R [0], which was also mentioned in the direct insert sort that was written earlier, and did not understand the effect of its terminating sign, but here it is perfectly clear.

  

Algorithm exercise--sequential lookup

Related Article

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.