Various methods of sorting arrays in php_PHP tutorial
Jul 13, 2016 pm 04:55 PMWhen we learn programming, we know that sorting methods include bubble sorting, quick sorting, selection sorting, etc., and the simultaneous sorting method Divided into two categories internal and external.
Sort method of array
Divided into two categories:
1. Internal (memory) sorting
2. External sorting (the amount of data is too large to be stored in the memory, so external sorting is required
Storage)
----------------
Sorting is a basic skill for a programmer
1: Internal sorting
(1) Commutative sorting
1. Bubble method
The default transfer of arrays is value transfer, not address transfer
We will talk about object types later. The default transfer of objects is address transfer
//Optimize, encapsulate the bubble sorting method into a function to facilitate future use
The code is as follows
|
Copy code
|
||||
function bubbleSort(&$myarr){ $temp=0;//Define an intermediate variable
for($i=0;$i<count($myarr)-1;$i++){ for($j=0;$j<count($myarr)-1-$i;$j++){ if($myarr[$j]>$myarr[$j+1]){ $temp=$myarr[$j]; |
$myarr[$j]=
}
3. Selection sorting method The code is as followsCopy code
|
function selectSort(&$myarr){
$temp=0;
for($i=0;$i<count($myarr)-1;$i++){<🎜>
//Assume $i is the smallest number<🎜>
$minval=$myarr[$i];<🎜>
//Record the subscript of the smallest number I think <🎜>
$minIndex=$i;<🎜>
<🎜>for($j=$i+1;$j<count($myarr);$j+ <🎜>
+){<🎜>
//Explain that the minimum value we think is not the minimum<🎜>
if($minval>$myarr[$j]){
$minval=$myarr[$j];
$minIndex=$j;
}
}
//Final exchange
$temp=$myarr[$i];
$myarr[$i]=$myarr[$minIndex];
$myarr[$minIndex]=$temp;
}
}
4. Insertion sort method
Query speed: bubble sorting method<selection sorting method<insertion sorting method
My personal favorite is exchange sorting, which is what everyone calls the bubble sorting method. This method is easier to use, but it is not suitable for sorting large amounts of data.
http://www.bkjia.com/PHPjc/631630.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631630.htmlTechArticleWhen we learn programming, we know that sorting methods include bubble sorting, quick sorting, selection sorting, etc. At the same time Sorting methods are divided into two categories: internal and external. The sorting method of the array is divided into two...
|
|

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
