PHP selection sorting method to implement array sorting instance analysis, array instance analysis _php Tutorial

Source: Internet
Author: User

PHP selection sorting method to implement array sorting example analysis, array instance analysis


In this paper, we analyze the method of sorting the array by choosing the method of PHP. Share to everyone for your reference. The specific analysis is as follows:

The basic idea of choosing a sorting method is to illustrate it directly with a case, such as an array $arr = Array (2,6,3,9), from large to small.

First big loop: It first assumes $arr[0] is the maximum value, then compares it with $arr[1]~ $arr [3], and if it is larger, it is exchanged, the process is such (2,6,3,9)---2 and 6 than---> (6,2,3,9)---6 and 3--- > (6,2,3,9)---6 and 9 than---> (9,2,3,6). Note that the subscript here also needs to change.

Second big cycle: assuming $arr[1] Max (excluding $arr[0]), compared with $arr[2]~ $arr [3], the process is such (9,2,3,6)----2 and 3 than----> (9,3,2,6)---3 and 6 than---> ( 9,6,2,3).

The third big cycle: assuming $arr[2] max, compared to $arr[3], the process is like this (9,6,2,3)---2 and 3 than---> (9,6,3,2)

In the same way, after N-1 cycles, you can arrange them.

The PHP code is as follows, and here is the same wrapper with the function

<?phpfunction selectsort (& $arr) {for ($i =0; $i
 
  <$arr[$j]){ $arr[$j]="$arr[$i];" $arr[$i]="$max;" }="" return="" $arr;}$myarr="array(2,6,3,9);selectSort($myarr);echo">
  
  ";p Rint_r ($myarr);? >
 

Code Analysis:

First big cycle:

$i =0 Array (2,6,3,9)
$j = 1, performs 2 and 6 ratios: becomes $arr[0]=6, $arr [1]=2, $max =6 (6,2,3,9)
$j = 2, perform 3 and 6 ratio: do not execute
$j = 3, performs 9 and 6 ratios: becomes $arr[0]=9, $arr [3]=6, $max =9 (9,2,3,6)

Second big cycle:

$i =1, $max = $arr [1]=2, Array (9,2,3,6)
$j = 2, performs 3 and 2 ratios: becomes $arr[1]=3, $arr [2]=2, $max =3 (9,3,2,6)
$j = 3, performs 6 and 3 ratios: becomes $arr[1]=6, $arr [3]=3, $max =6 (9,6,2,3)

The third big cycle:

$i =2, $max = $arr [2]=2, Array (9,6,2,3)
$j = 3, performs 3 and 2 ratios: becomes $max[2]=3, $arr [3]=2, $max =3 (9,6,3,2)

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/959115.html www.bkjia.com true http://www.bkjia.com/PHPjc/959115.html techarticle PHP Selection Sorting method to achieve array Sorting example analysis, array instance analysis in this paper, we analyze the method of selecting sorting by PHP to realize array sorting. Share to everyone for your reference. Specific analysis ...

  • 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.