The three sorting methods of arrays in PHP are Shared

  • 2020-05-17 04:53:52
  • OfStack

Bubble sort
Description: find the largest number, arrange it to the end, and keep looking

Ex. :
 
$arr = array(3,5,-1,0,2); 
for($i=0;$i<count($arr)-1;$i++){ 
for($j=0;$j<count($arr)-1-$i;$j++){ 
if($arr[$j]>$arr[$j+1]){ 
$temp = $arr[$j]; 
$arr[$j]=$arr[$j+1]; 
$arr[$j+1]=$temp; 
} 
} 
} 

To understand:
3,5, minus 1,0,2
// start with the first number and compare it later. If it is larger than the next number, adjust the position to the next number
// the first time, 3 is less than 5, so it doesn't change
// the second time, 5 is greater than negative 1, so it becomes
3, minus 1,5,0,2
// the third time, 5 is greater than 0
Minus 1,0,5,2
// the fourth time, 5 is greater than 2
Minus 1,0,2,5
At this point, the inner loop is completed for 1 time. At this point, the last number completes sorting and will not participate in the next time
3, minus 1, 0, 2, 5 the second outer loop begins the first time: 3 is greater than minus 1
Minus 1,3,0,2,5
The second time: 3 is greater than 0
Minus 1,0,3,2,5
The third time: 3 is greater than 2
Minus 1,0,2,3,5
So we're done sorting the next two digits, and so on
Minus 1,0,2,3,5
2. Select sort
Note: assume that the first number is the smallest number, and then compare the following Numbers with it. If the assumed number is not the smallest number, then swap it with the smallest number
 
$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; 
} 

To understand:
2,1, minus 1,3,0
// assume that the first number, 2, is the minimum value, and then compare the following Numbers with 2 in order to find the smallest number
Process:
1 is less than 2, so minval is equal to 1
Minus 1 is less than 1, so minval is equal to minus 1
3 is greater than negative 1, so it doesn't change
So 0 is greater than negative 1, so it doesn't change
So now we have the smallest number in the array which is minus 1
You switch the negative 1 and the 2 and you're done sorting the first number
So now the array becomes
Minus 1,1,2,3,0
Now the first number, minus 1, is already in order, so I'm not going to compare it, so I'm going to keep going
Now let's say minval=1
2 is greater than 1, and it doesn't change
3 is greater than 1. It doesn't change
0 is less than 1, so minval is equal to 0
Now loop 1, switch the 0 and 1 to complete the sorting of the second number
So now the array becomes
Minus 1,0,2,3,1
// the following is the same as above...

3. Insertion sort

Note: if the first number in an array is assumed to be a separate ordered array, then compare the last number with it. If the last number is smaller than the assumed number, move the smaller number back, and finally move the number to the front
 
$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; 
} 

To understand:
2,1, minus 1,3,0
// the first time, first save the number to be inserted 1 as insertval, and then compare insertval with 2, 1 is less than 2, so move 2 back to the following figure
2,2, minus 1,3,0
// at this point, there is no number in front of 2, insertindex=0, so the comparison is done, so insert insertval into the position you are looking for. Becomes the following picture
1,2, minus 1,3,0
// at this point, 1,2 becomes an ordered array
// the second time, save the number to be inserted -1 is insertval, then compare insertval with 2, -1 is less than 2, so move 2 back to the following figure
1,2,2,3,0
// at this point, compare insertval with 1 again, negative 1 is less than 1, then move back negative 1 to the following figure (this is the process of comparing the number to be inserted with the previous ordered array)
1,1,2,3,0
// at this point, insertindex comes to an end, so insert insertval into this position
Minus 1,1,2,3,0
// follow as above

Related articles: