Here we introduce some of the commonly used sorting methods, sorting is a programmer's basic skills, so-called sorting is a set of data, in a certain order of the process.
Charging efficiency look
Bubble sorting < selection sorting < insertion sorting sorted by two main categories:
Internal sorting method
Exchange-Type sorting method
Bubbling method
Basic idea:
Bubble Sort Method
Case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 |
Simple. $arr =array (0,5,-1);
Now let's wrap the function as a function and use it later The array is passed by default to the value, not the address,& is the address character Function Bubble (& $myarr) { This is an intermediate variable $temp = 0; We're going to get the array from small to large
Outer Loop for ($i =0; $i <count ($myarr)-1; $i + +) { for ($j =0; $j <count ($myarr)-$i; $j + +) { If the previous number is larger than the number that follows, swap if ($myarr [$j]> $myarr [$j +1]) { $temp = $myarr [$j]; $myarr [$j]= $myarr [$j +1]; $myarr [$j +1]= $temp; } } } } Use functions to sort Bubble ($arr); Output Print_r ($arr); |
Analysis diagram
Analysis diagram
Select Sort method
Select sort
Case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 |
Select Sort method Function Selectsort (& $arr) { Defining intermediate variables $temp = 0;
for ($i =0; $i <count ($arr)-1; $i + +) { Let's say $i is the smallest number. $minVal = $arr [$i]; Record the subscript of the minimum number I think $minIndex = $i; for ($j = $i +1; $j <count ($arr); $j + +) { Describe the minimum value we think, not the smallest if ($minVal > $arr [$j]) { $minVal = $arr [$j]; $minIndex = $j; } } Last Exchange $temp = $arr [$i]; $arr [$i]= $arr [$minIndex]; $arr [$minIndex]= $temp; } } |
Flow chart
Flow chart
Insert Sort method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Insert Sort method Function Insertsort (& $arr) { The default subscript is 0, this number is already ordered for ($i =1; $i <count ($arr); i++) { $insertVal is the number that is ready to be inserted $insertVal =%arr[$i]; Prepare to compare with $insertindex first. $insertIndex = $i-1; If this condition is met, it is stated that we have not found the proper location while ($insertIndex >=0&& $insertVal < $arr [$insertIndex]) { And move the number back $arr [$insertIndex +1]= $arr [$insertInde]; $insertIndex--; } The insertion then gave $insertval the proper location. $arr [$insertIndex +1]= $insertVal; } } |
Flow chart
PHP Quick Sort Code
1 2 3 4 5 6 7 8 9 All 16 ( ) , , , , ,
|
function QuickSort ($LEFL, $right,& $array) { $l = $LEFL; $r = $right; $pivot = $array [($LEFL + $right)/2];
Whlie ($l < $r) { while ($array [$l]< $pivot) $l + +; while ($array [$r]> $pivot) $r-;
if ($l >= $r) break;
$temp = $array [$l]; $array [$l]= $array [$r]; $array [$r]= $temp;
if ($array [$l]== $pivot)--$r; if ($array [$r]== $pivot) + + $l; } if ($l = = $r) { $l + +; $r--; } if ($left < $r) QuickSort ($left, $r, $array); if ($right > $l) quickSort ($l, $right, $array); } |
Create 200,000 of data dynamically
1 2 3 |
for ($i =0; $I <200000; $i + +) { $array [$i]=rand (0,3000); } |
Analysis flowchart
Quick Sort Method
Disclaimer: Article content from the Internet, sharing in the internet!
PHP Sort Introduction _ Bubble Sort _ Choose sorting method _ Insert Sort Method _ Quick Sort method