Sort the insertion sort-php

Source: Internet
Author: User

1<?PHP2     Header("Content-type:text/html;charset=utf-8");3 4     /*5 Insert Sort idea:6 Source: 7 17 8 1. First: Two variables required9 $insertVal = "To save the value of the currently traversed numeric element"Ten $index = "To hold the subscript for the previous value of the currently traversed numeric element value" One  A  - 2. If the conditions are met - condition: "$index greater than 0" and "Previous value element value" is greater than "current numeric element value " the                  - satisfying a condition action sets the $arr [$index +1] (that is, $arr[$i] that is the current numeric element value) to "the previous value of the value of the currently traversed numeric element", and then $index-1 - The condition operation is not satisfied $arr [$index +1] = $insertVal (the value of the currently traversed value element is set to the value of the numeric element that has been traversed to Operation 1 o'clock); -          + Example -  + First time: A $insertVal = Ten at $index = 1 -  - do not meet the loop -  - $arr [$index +1] = $insertVal//10 7 1 -  in results: 7 1 -              to Second time: + $insertVal = 7 - $index = 0 the  * Meet the Loop $ //1: (0>=0 && 10>7)Panax Notoginseng ($index >=0 && $arr [$index]> $insertVal) - $arr [$index +1] = $arr [$index]; Copy the previous value to the current value the $index--; --After the value is-1 +                      A 1 results in loop: 1 the                  +  - $arr [$index +1] = $insertVal//7 1 $  $ results: 7 1 -  - third time: the $insertVal = 1 - $index = 1Wuyi  the Meet the Loop - //1: (1>=0 && 10>1) 2: (0>=0 && 7>1) 3: ( -1>=0 && 7>1) xxx Wu ($index >=0 && $arr [$index]> $insertVal) - $arr [$index +1] = $arr [$index]; Copy the previous value to the current value About $index--; 1: (--After the value is 0) 2: (--After the value is-1) $                      - 1 results in loop: 1 -                  -  A $arr [$index +1] = $insertVal//1 7 +  the results: 1 7 -          $     */ the  the  the  the      -     $arr=Array(10, 7, 1); in  the     functionSearch (&$arr){ the          About         //from $i = 1 This number is added to an ordered array the          for($i= 0;$i<Count($arr);$i++){ the              the             //extracts the first value of an array for saving +             $insertVal=$arr[$i]; -              the             //traverse the subscript of the previous value of the current value (subscript of the left value)Bayi             $index=$i-1; the              the             /* - $index >=0 to prevent cross-table crossing - $arr [$index] > $insertVal the previous number compared to the current value ($arr [$i] = $insertVal) to replace the             */ the              while($index>=0 &&$arr[$index]>$insertVal){ the                  the                 $arr[$index+1] =$arr[$index]; -                 $index--;//$index--means to move forward, because I don't know if there are any numbers ahead . the             } the  the             //put the $insertval value in the $index +1 position94             $arr[$index+1] =$insertVal; the         } the     } the 98Search$arr); About  -     foreach($arr  as $value){101         Echo $value. ' <br> ';102     }103 104?>

Sort the insertion sort-php

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.