Sort by Select Sort-php

Source: Internet
Author: User

1<?PHP2     //set the request header3     Header("Content-type:text/html;charset=utf-8");4 5     /*6 Choose the idea of sorting:7 For example: 10 7 2 6 is an array with the name arr8 requirements: Sort from small to large9 Ten 1th Round One arr[0] to arr[arr.length-1], select the minimum value, that is, 2, and arr[0] Exchange values A Draw 2 7 6 -  - 2nd round the arr[1] to arr[arr.length-1], select the minimum value, that is, 6, and arr[1] Exchange values - Draw 2 6 7 -  - 3rd round + arr[2] to arr[arr.length-1], select the minimum value, that is, 7, and arr[2] Exchange values - Draw 2 6 7 +  A         .... at Nth round - that the last value does not need to compare itself -  -  - Conclusion: - after each round of comparisons, the next round will not let arr[start] participate in the comparison in      -     */ to  +  -     $arr=Array(10,7,2,6,3,1,5,6,7,3,-2,-2,-3); the     $arr _count=Count($arr); *  $     Var_dump($arr);Panax Notoginseng  -     //$arr _count-1 means that the last number doesn't have to be compared . the      for($i= 0;$i<$arr _count-1;$i++){ +          A         $min=$arr[$i];//Minimum Value the         $min _index=$i;//Subscript of the minimum value +  -          for($j=$i+1;$j<$arr _count;$j++){ $              $             if($min>$arr[$j]){ -                 $min=$arr[$j]; -                 $min _index=$j; the             } -         }Wuyi  the         if($min _index==$i){ -             Continue; Wu         } -  About         $temp=$arr[$i]; $         $arr[$i] =$min; -         $arr[$min _index] =$temp; -  -     } A      +     Echo' ================================= '; the  -     Var_dump($arr); $  the  the?>

Sort by Select 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.