PHP's four entry-level sorting algorithms

Source: Internet
Author: User
This article is to share the content of PHP four of the entry-level ranking algorithm, has a certain reference value, the need for friends can refer to

four entry-level sorting algorithms:bubble Sort, select sort, insert sort, quick sort.

$a = [5,2,4,1,6,5,3,8];function A ($a) {//bubble sort//$temp = 0;            for ($i =0; $i <count ($a)-1; $i + +) {for ($j =0; $j <count ($a)-$i, $j + +) {if ($a [$j]> $a [$j +1]) {            $temp = $a [$j];            $a [$j] = $a [$j +1];            $a [$j +1] = $temp; }}} print_r ($a);}    A ($a); function order ($arr) {//select sort//define intermediate variable $temp = 0;    $count = count ($arr);        for ($i =0; $i < $count-1; $i + +) {//define minimum position $minIndex = $i;            for ($j = $i +1; $j < $count; $j + +) {if ($arr [$j] < $arr [$minIndex]) {$minIndex = $j;            }} if ($i! = $minIndex) {$temp = $arr [$i];            $arr [$i] = $arr [$minIndex];        $arr [$minIndex] = $temp; }} return $arr;} echo "<pre>";    Print_r (Order ($a)), function Swap (array & $arr, $a, $b) {//Direct insert Sort $temp = $arr [$a];    $arr [$a] = $arr [$b]; $arr [$b] = $temp;}    function Insertsort (array & $arr) {$count = count ($arr); ArrayThe first element as an already existing ordered table for ($i = 1; $i < $count; $i + +) {$temp = $arr [$i];       Set Sentinel for ($j = $i-1; $j >= 0 && $arr [$j] > $temp; $j-) {$arr [$j + 1] = $arr [$j];      Record Move} $arr [$j + 1] = $temp; Insert into the correct position}}insertsort ($a); Var_dump (' <pre> ', $a), function Quick_sort ($arr)//functions for fast ordering {//Determine if the parameter is an array if (!is_    Array ($arr)) return false;    Recursive exit: Array length is 1, return array directly $length =count ($arr);    if ($length <=1) return $arr;    array element has multiple, then define two empty array $left = $right =array ();            Using a For loop, use the first element as the object of comparison for ($i =1; $i < $length; $i + +) {//To determine the size of the current element if ($arr [$i]< $arr [0]) {        $left []= $arr [$i];        }else{$right []= $arr [$i];    }}//Recursive call $left =quick_sort ($left);    $right =quick_sort ($right); Merge all results into return Array_merge ($left, Array ($arr [0]), $right);} Var_dump (' <pre> ', $a);//Call echo "<pre>";p rint_r (Quick_sort ($a));

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.