PHP Quick Sort

Source: Internet
Author: User

Direct quick Sort is very simple, is an improvement of bubble sort, is the first time complexity reaches O (nlogn) algorithm, the worst time complexity is still O (n^2), it is also one of the ten greatest algorithms of the 21st century.

Quick sort there are three kinds, namely the balance of the fast row, the external fast row and the three-way base fast row, I do not separately introduced, the interest of the pot friends can see here.

Direct fast sort code directly on PHP

function QuickSort ($a) {if (count ($a) <=1) return $a;  $l = $r = Array ();  $n = Array_shift ($a);  foreach ($a as $v) $v < $n Array_push ($l, $v): Array_push ($r, $v); Return Array_merge (QuickSort ($l), Array ($n), QuickSort ($r));}


Usage:

Echo (Implode (",", QuickSort ($a)));//$a is an unordered array, for example $a = range (1, 100); Shuffle ($a);


Hey, there's a better thought welcome comment

This article is from "A Tangled PHP programmer" blog, please be sure to keep this source http://johnsylm.blog.51cto.com/1480623/1429380

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.