When we learn the procedure, we know that the sorting method, the fast sorting method, the selection sorting method, and the sorting methods are divided into two classes inside and outside the class.
Sorting Methods for arrays
Divided into two in the class:
1, internal (memory) Department sort
2, external sorting (large data volume, memory, need to rely on external
Storage
----------------
Sort sorting is the basic skill of a programmer
One: Internal sorting
(1) Exchange sort
1. Bubbling method
The default pass of an array is a value pass, not an address pass
The object type is followed by the object's default delivery, which is the address passing
optimization, the bubble sorting method is encapsulated into a function for later use
The code is as follows |
Copy Code |
Function Bubblesort (& $myarr) { $temp =0;//define an intermediate variable Outer Loop for ($i =0; $i<> for ($j =0; $j $j + +) { if ($myarr [$j]> $myarr [$j +1]) { $temp = $myarr [$j]; $myarr [$j]= $myarr [$j +1]; $myarr [$j +1]= $temp; } } } } $arry =array (2,6,-4,7,9,0); Bubblesort ($arry); Print_r ($arry); |
2. Quick Sort method (magical fast, involving recursion)
3. Select the Sorting method
The code is as follows |
Copy Code |
Function Selectsort (& $myarr) { $temp = 0; for ($i =0; $i<> Let's say $i is the smallest number. $minval = $myarr [$i]; Record the subscript of the minimum number I think $minIndex = $i; for ($j = $i +1; $j +){ Describe the minimum value we think, not the smallest if ($minval > $myarr [$j]) { $minval = $myarr [$j]; $minIndex = $j; } } Last Exchange $temp = $myarr [$i]; $myarr [$i]= $myarr [$minIndex]; $myarr [$minIndex]= $temp; } } |
4. Insert Sort method
Query speed: Bubble Sort method<><>< p=""><><>
Personal favorite is the exchange sort, that is, everyone said bubble sorting method, this method is more useful, but not suitable for large data volume sorting.
http://www.bkjia.com/PHPjc/631630.html www.bkjia.com true http://www.bkjia.com/PHPjc/631630.html techarticle when we learn the procedure, we know that the sorting method, the fast sorting method, the selection sorting method, and the sorting methods are divided into two classes inside and outside the class. The sorting method of the array is divided into two in ...