First, bubble Sort method
Description: Find the maximum number, arrange it to the last face, and then continue looking for
Cases:
$arr = Array (3,5,-1,0,2);
for ($i =0; $i <>
for ($j =0; $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 beginning of the first number, if it is larger than the following number
The first time, 3 is less than 5, then 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 the inner loop, at which point the last number is sorted and the next one will not participate
3,-1,0,2,5 second outer loop starts 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 following two-digit sort, and then
-1,0,2,3,5
Second, the choice of sorting method Description: First assume that the first number is the smallest number, and then the subsequent number of the comparison with it, if the assumed number is not the smallest number, it and the back of the smallest number swap position
$arr =array (2,1,-1,3,0);
for ($i =0; $i <>
$minval = $arr [$i];
$minindex = $i;
For ($j =1+ $i; $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
First assume that the first number 2 is the minimum value, and the number after it is compared with 2, to find the smallest number
Process:
1 is less than 2, then minval=1
-1 less than 1, then minval=-1
3 is greater than-1, unchanged
0 is greater than-1, unchanged
So now we've found the smallest number in the array--1.
The first number is sorted by swapping positions 1 and 2.
So now the array becomes
-1,1,2,3,0
Now the first number-1 is already ordered, 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, then minval=0
Now once the loop is complete, change the position of 0 and 1 to finish the second number.
So now the array becomes
-1,0,2,3,1
The following push method is the same as above ...
The insertion sorting method Description: First assume that the first number in an array is a separate ordered array, and then a number with it "here with it I grow, it becomes them" to do the comparison, if the subsequent number is smaller than the hypothetical number, then the small number of the next move, and finally move the number to the front
$arr =array (2,1,-1,3,0);
for ($i =1; $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, save the number of 1 to be inserted as insertval, then compare Insertval with 2, 1 is less than 2, so move 2 back, turn into the following figure
2,2,-1,3,0
At this time there is no number in front of 2, insertindex=0, so the comparison is completed, then the Insertval inserted into the search for this position. Become as
1,2,-1,3,0
At this point, you become an ordered array
Second, first save the number to be inserted-1 for Insertval, then take insertval and 2 to compare, 1 is less than 2, so put 2 back, turn into as
1,2,2,3,0
At this point, then take insertval and 1 to do the comparison, 1 less than 1, then the 1 is shifted back, so as (this is a number to be inserted with the previous sequence of the sequential array of the process)
1,1,2,3,0
At this point, the insertindex is over, so insert insertval into the position
-1,1,2,3,0
Behind the push method above
From Bell's technical blog
http://www.bkjia.com/PHPjc/478255.html www.bkjia.com true http://www.bkjia.com/PHPjc/478255.html techarticle first, the bubble sorting method Description: Find the largest number, arrange to the last side, and then continue to find examples: $arr = array (3,5,-1,0,2); for ($i =0; $icount ($arr)-1; $i + +) {for ($j =0; $jcount ($arr )-$i ...