Home > Backend Development > PHP Tutorial > Examples of four basic sorting algorithms in PHP

Examples of four basic sorting algorithms in PHP

WBOY
Release: 2016-08-08 09:29:46
Original
1293 people have browsed it

Overview: PHP’s four basic sorting algorithms include: bubble sort, quick sort, selection sort, and insertion sort.

1. Bubble sort

Idea analysis: In a set of numbers to be sorted, compare and adjust the two adjacent numbers in sequence from front to back for the sequence that has not yet been sorted. , let the larger numbers sink and the smaller numbers rise. That is, whenever two adjacent numbers are compared and it is found that their ordering is opposite to the ordering requirement, they are swapped.
Code implementation:
$arr=array(1,43,54,62,21,66,32,78,36,76,39);
function bubbleSort($arr)
{
$len= count($arr);
//This layer of loop controls the number of rounds that need to bubble
for($i=1;$i<$len;$i++)
{ //This layer of loop is used to control each round of bubbling The number of times a number needs to be compared
for ($k=0;$k<$len-$i;$k++)
{
if($arr[$k]>$arr[$k+1])
{
                    $tmp=$arr[$k+1];
return $ arr;
}


2. Selection sort

Code implementation:

function selectSort($arr) {//Double loop is completed, the outer layer controls the number of rounds, and the inner layer controls the number of comparisons $len =count($arr); for($i=0; $i<$len-1; $i++) {                                                                                                                                                                                                     ​=$i+1; $j<$len; $j++) {
                                                                                                                                                                {, // Comparison, find a smaller one, record the position of the minimum value; and use the known minimum value to compare at the next comparison.
                                                                                                                                                                             If it is found that the position of the minimum value is different from the currently assumed position $i, the positions can be interchanged. ! If ($ p! = $ I) {
$ tMP = $ arr [$ p];
$ arr [$ p] = $ arr [$ i];
$ arr [$ i] = $ tmp;
} }
// Return the final result
Return $ arr;
}


3. Insert sorting


thinking analysis: In a set of sorting Insert the nth number into the previous ordered number so that these n numbers are also in order. Repeat this cycle until everything is in order.

Code implementation:
function insertSort($arr) {
$len=count($arr);
for($i=1, $i<$len; $i++) {
$tmp = $arr [$ i]; 内 // Inner circulation control, compare and insert
for ($ j = $ i-1; $ j & gt; = 0; $ j-) {
if ($ tmp & lt; j]) {
                                                                                                                                                                           ] = $ Tmp;
} else {
// If you encounter an element that does not need to move, because it is already sorted as an array, then the previous one does not need to be compared again.
                                                                                              break; The first element or the last element. Through one scan, the column to be sorted is divided into two parts, one part is smaller than the reference element, and the other part is greater than or equal to the reference element. At this time, the base element is at its correct position after sorting, and then the two divided parts are sorted recursively in the same way.



Code implementation:


function quickSort($arr) {
//First determine whether you need to continue

$length = count($arr);

if($length <= 1) {         return $ arr; }

//Select the first element as the base

$base_num = $arr[0];

//Traverse all elements except the ruler and put them into two arrays according to the size relationship

//Initialize two Array $left_array = array(); // Smaller than the base $right_array = array(); // Greater than the base for ($i=1; $i<$length; $i++) { if($base_num > $right_array[] = $arr[$ i];
                                                                                                                                                                                                                                  . /Merge
return array_merge($left_array, array($base_num), $right_array);
}

Reference:

http://www.evget.com/article/2015/2/11/22149.html


http://www.evget.com/product/2605



The above has introduced examples of PHP's four basic sorting algorithms, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template