Principles and usage of PHP selection sorting

Source: Internet
Author: User
This article mainly introduced the PHP choice sorting method realizes the array sorting method, the example analyzes the choice sorting principle and the concrete execution procedure, has the certain reference value, the need friend may refer to the next

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 <count ($arr); $i + +) {$max = $arr [$i]; for ($j = $i +1; $j <count ($ ARR), $j + +) {  if ($max < $arr [$j]) {  $max = $arr [$j];  $arr [$j] = $arr [$i];  $arr [$i] = $max;  } }} return $arr;} $myarr = Array (2,6,3,9); Selectsort ($myarr); echo "<pre>";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)

Summary : The above is the entire content of this article, I hope to be able to help you learn.

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.