Relive PHP's quick sort

Source: Internet
Author: User

Tags: unit src div php otherwise get traverse return before

Rationale: Select any element in the current array (usually the first one) as the standard, create a new two empty array before and after the current array, and then iterate through the current array, if the element value in the array is less than or equal to the first element value, put it in an empty array, or put it behind an empty array.

1 //Quick Sort2 functionQuick_sort ($arr) {3     //get the number of array cells4     $count=Count($arr);5     //determining the length of an array6     if($count<= 1) {7         return $arr;8}Else {9         //define two empty arraysTen         $before=$after=Array(); One         //iterating through an array A          for($i= 1;$i<$count;$i++) { -             //Judging by the first element as a criterion -             if($arr[$i] <=$arr[0]) { the                 $before[] =$arr[$i]; -}Else { -                 $after[] =$arr[$i]; -             } +         } -         //Recursive invocation +         $before= Quick_sort ($before); A         $after= Quick_sort ($after); at         //Merging Arrays -         return Array_merge($before,Array($arr[0]),$after); -     } - } -Test - $arr=Array(16, 9, 3, 12, 88, 19, 18, 16); in Var_dump(Quick_sort ($arr));
 
  



Relive PHP's quick sort

Related Article

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.

Tags Index: