PHP array sorting various methods _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.