Three sorting methods for arrays in PHP sharing _php tips

Source: Internet
Author: User
First, bubble Sort method
Description: Find the maximum number, arrange to the last side, and then continue to find

Cases:
Copy Code code as follows:

$arr = Array (3,5,-1,0,2);
For ($i =0 $i <count ($arr)-1; $i + +) {
For ($j =0 $j <count ($arr)-$i; $j + +) {
if ($arr [$j]> $arr [$j +1]) {
$temp = $arr [$j];
$arr [$j]= $arr [$j +1];
$arr [$j +1]= $temp;
}
}
}

Understand:
3,5,-1,0,2
Compare from the first number to the back if the number is larger than the following
The first time, 3 is less than 5, so unchanged
The second time, 5 is greater than-1, then becomes
3,-1,5,0,2
Third time, 5 greater than 0
3,-1,0,5,2
Fourth time, 5 greater than 2
3,-1,0,2,5
This completes an inner loop, at which point the last number completes the sort, and the next time it does not participate
3,-1,0,2,5 second outer circulation start first: 3 Greater than-1
-1,3,0,2,5
Second time: 3 greater than 0
-1,0,3,2,5
Third time: 3 Greater than 2
-1,0,2,3,5
This completes the two-digit order of the following, and the next analogy
-1,0,2,3,5
second, the choice of sorting method
Note: Let's assume that the first number is the smallest number, and then compare the following number to it, if the number is not the smallest number, it and the smallest number of the following exchange position
Copy Code code as follows:

$arr =array (2,1,-1,3,0);
For ($i =0 $i <count ($arr)-1; $i + +) {
$minval = $arr [$i];
$minindex = $i;
For ($j =1+ $i; $j <count ($arr); $j + +) {
if ($arr [$j]< $minval) {
$minval = $arr [$j];
$minindex = $j;
}
}
$temp = $arr [$i];
$arr [$i] = $arr [$minindex];
$arr [$minindex] = $temp;
}

Understand:
2,1,-1,3,0
Let's assume that the first number 2 is the minimum, and the following numbers are compared to 2 in turn, looking for the smallest number.
Process:
1 is less than 2, so minval=1
-1 less than 1, then minval=-1
3 Greater than-1, unchanged
0 greater than-1, unchanged
So now we've found the smallest number in the array--1.
The first number is sorted by replacing the 1 with the 2 position.
So now the array becomes
-1,1,2,3,0
Now the first number-1 is already in order, so do not participate in the comparison, the back side continues
Now suppose Minval=1
2 is greater than 1, unchanged
3 is greater than 1, unchanged
0 is less than 1, so minval=0
Now once the loop is done, replace 0 and 1 to complete the order of the second number.
So now the array becomes
-1,0,2,3,1
The following pushing method is the same as above ...

third, insert sorting method

Description: First assume that the first number in an array is a separate ordered array, then compare the number of the following with its "growth as it goes with it" here, and if the following number is smaller than the number assumed, move the smaller number back, and then move the number to the front.
Copy Code code as follows:

$arr =array (2,1,-1,3,0);
for ($i =1; $i <count ($arr); $i + +) {
$insertval = $arr [$i];
$insertindex = $i-1;
while ($insertindex >=0 && $insertval < $arr [$insertindex]) {
$arr [$insertindex +1]= $arr [$insertindex];
$insertindex--;
}
$temp = $arr [$i];
$arr [$insertindex +1]= $insertval;
}

Understand:
2,1,-1,3,0
For the first time, first save the number of 1 to be inserted as insertval, then compare Insertval with 2, 1 is less than 2, so move the 2 back, and turn the figure below.
2,2,-1,3,0
At this point, there is no number in front of 2, insertindex=0, so the comparison is complete, then insert insertval into this position. into the following figure
1,2,-1,3,0
At this point, the 1,2 becomes an ordered array
The second time, first save the number to be inserted-1 for insertval, and then compare Insertval with 2, 1 less than 2, so move 2 back, into the following figure
1,2,2,3,0
At this point, compare the Insertval with 1, 1 is less than 1, then move 1 back to the following figure (this is the process of comparing the number of inserts to the preceding ordered array)
1,1,2,3,0
At this point, the insertindex is over, so insert insertval into that position
-1,1,2,3,0
Behind the push method above

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.