php--bubble, select, insert Sort method

Source: Internet
Author: User

Use PHP to implement three of common sorting methods:

Bubble, select, Insert, the best is the insertion sort, I took the insertion sort of process to draw down:

Flowchart for inserting the sorting method:

To insert a sorted code:

Function Insertsort (& $arr) {    for ($i =1; $i <count ($arr); $i + +) {        //with inserted value        $insertVal = $arr [$i];        The position to compare subscript        $insertIndex = $i-1;        while ($insertIndex >=0 && $insertVal < $arr [$insertIndex]) {            //If the subscript of the comparison number is greater than or equal to 0, the inserted value is smaller than the value being compared, The value to be compared will be shifted back            $arr [$insertIndex +1] = $arr [$insertIndex];            $insertIndex--;        }        Insert Insertval        if ($insertIndex +1! = $i) {            $arr [$insertIndex +1] = $insertVal;        }}    }

The following three kinds of sorting code:

1 <?php
 2/** 3 * Created by Phpstorm. 4 * User:xxx 5 * DATE:2016/10/12 6 * time:21:38 7 */8//Bubble sort 9 function Maopao_sort (& $arr) {10//outer loop control trip         Count only Count ($arr)-1 times to complete a sort for ($i =0; $i <count ($arr)-1; $i + +) {12//inner loop control each trip to find the largest number: Ccount ($arr)-$i 13 for ($j =0; $j <count ($arr)-$i, $j + +) {if ($arr [$j] > $arr [$j +1]) {$temp = $arr [$ J+1];16 $arr [$j +1] = $arr [$j];17 $arr [$j] = $temp; 18}19}20}21} 22 23//Select Sort function selectsort (& $arr) {$i =0; $i <count ($arr)-1; $i + +) {26//assumed minimum number of $m             inval = $arr [$i];28//Minimum number of subscripts $minIndex = $i; ($j = $i +1; $j <count ($arr); $j + +) {31         if ($minVal > $arr [$j]) {$minVal = $arr [$j];33 $minIndex = $j; 34}35        }36 if ($i! = $minIndex) {PNS $temp = $arr [$i];38 $arr [$i] = $minVal; 39     $arr [$minIndex] = $temp; 40}41}42}43 44//Insertion sort (small-to-Large) function Insertsort (& $arr) {$ for ($i =1; $i <count ($arr); $i + +) {47//with inserted value $insertVal = $arr [$i];49//The position to compare is subscript $insertIndex = $i -1;51 whil E ($insertIndex >=0 && $insertVal < $arr [$insertIndex]) {52//If the subscript of the comparison is greater than or equal to 0, and the value inserted is smaller than the value being compared, the value to be compared will be moved back $arr [$insertIndex +1] = $arr [$insertIndex];54 $insertIndex--;55}56//Insert Insertv al57 if ($insertIndex +1! = $i) {$arr [$insertIndex +1] = $insertVal,}60}61}62 $a rr = Array (10,2,0,-23,90,-100,400),//maopao_sort ($arr),//selectsort ($arr), Insertsort ($arr), Echo var_dump ( $arr). " <br> "; Print_r ($arr);

Quick Sort Method:

1 <?php 2 function quickSort (& $arr) {3     if (count ($arr) >1) {4         $k = $arr [0]; 5         $x =array (); 6         $y = Array (); 7         $_size=count ($arr); 8 for         ($i =1; $i <$_size; $i + +) {9             if ($arr [$i]<= $k) {ten                 $x []= $arr [$i];11             }elseif ($arr [$i]> $k) {                 $y []= $arr [$i];13             }14         }15         $x =quicksort ($x);         $y QuickSort ($y);         array_merge ($x, Array ($k), $y)     }else{19         return$arr;20     }21}22?>

php--bubble, select, insert Sort method

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.