PHP Quick Sort Method

Source: Internet
Author: User

1<?PHP2 functionQuickSort ($arr) {3     //determine if you need to proceed4     $length=Count($arr);5     if($length<= 1) {6         return $arr;7     }8     //If there is no return, the number of elements in the array is 1 extra and needs to be sorted9 //Select a rulerTen //Select the first element One     $base _num=$arr[0]; A     //iterates through all elements except the ruler, and fits into two arrays in relation to size - //initialization of two arrays -     $left _array=Array();//less than the ruler the     $right _array=Array();//larger than the ruler -      for($i= 1;$i<$length;$i++) { -         if($base _num>$arr[$i]) { -             //put in the left array +             $left _array[] =$arr[$i]; -}Else { +             //put in the right array A             $right _array[] =$arr[$i]; at         } -     } -     //The same sort method for the left and right arrays, respectively. - //Recursively call this function and record the result -     $left _array= QuickSort ($left _array); -     $right _array= QuickSort ($right _array); in      -     //Merge left ruler right to     return Array_merge($left _array,Array($base _num),$right _array); + } - $arr=Array(88, 1, 2, 5, 4, 3, 66, 0); the $res= QuickSort ($arr); * Print_r($res);

PHP Quick 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.