Home Backend Development PHP Tutorial Various methods of sorting arrays in php_PHP tutorial

Various methods of sorting arrays in php_PHP tutorial

Jul 13, 2016 pm 04:55 PM
php exist fast us sort array method have Know choose

When 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

$myarr[$j]= $myarr[$j+1];
The code is as follows
 代码如下 复制代码

function bubbleSort(&$myarr){
$temp=0;//定义一个中间变量
//外层循环
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]=
$myarr[$j+1];
$myarr[$j+1]=
$temp;
}

}
}
}

$arry=array(2,6,-4,7,9,0);
bubbleSort($arry);
print_r($arry);

Copy code

function bubbleSort(&$myarr){

$temp=0;//Define an intermediate variable
 代码如下 复制代码

function selectSort(&$myarr){
$temp=0;
for($i=0;$i<count($myarr)-1;$i++){
//假设$i就是最小的数
$minval=$myarr[$i];
//记录我认为的最小数的下标
$minIndex=$i;

for($j=$i+1;$j<count($myarr);$j+
+){
//说明我们认为的最小值,不是最小
if($minval>$myarr[$j]){

$minval=$myarr[$j];
$minIndex=$j;

}

}
//最后交换
$temp=$myarr[$i];
$myarr[$i]=$myarr[$minIndex];
$myarr[$minIndex]=$temp;
}

}

//Outer loop

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+1]= $temp;

}

} } $arry=array(2,6,-4,7,9,0); bubbleSort($arry); print_r($arry); 2. Quick sorting method (magically fast, involving recursion)
3. Selection sorting method
The code is as follows
Copy 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...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles