PHP Quick Sort

Source: Internet
Author: User

Fast sorting is commonly used in sorting, and efficiency is said to be good, and it uses a divide-and-conquer algorithm to achieve

Divide a large sequence into two smaller sequences that need to be sorted! How to divide, you need to find an element from the sequence as a reference element, it is common practice to take the first element as a reference element. When a sequence has only one element or 0 elements, it indicates that the sequence is sorted.

Steps:
1, using an element as the reference element
2, splits the sequence, forms the element which is less than the reference element and is greater than the reference element
3, respectively to the campus and more than two sequences using the same sorting algorithm to complete the sort (recursive call)
4, combine left, reference, and right elements to

<?php/** * Quick Sort * php Version 5.3.13  */header ("content-type:text/html;charset=utf-8"); function quicksort ($list {$len = count ($list);//define the recursive exit if ($len <= 1) {return $list;} $one = $list [0];//defines two sequences $left = $right = Array (); for ($i =1; $i < $len; + + $i) {if ($list [$i] < $one) {$left [] = $list [$i];} else{$right [] = $list [$i];}} $left = Quicksort ($left); $right = Quicksort ($right); $result = Array_merge ($left, Array ($one), $right); return $result;} $arr = Array (45,10,20,30,40,50,60); Echo ' before sort: ', Print_r ($arr); Echo ' 

In the process of running the program, because careless small wrong piece of code, and produce an error, let me feel the seriousness of the importance of the program, any of his happen we have to monitor.

Notice: Undefined offset:0 in D:\lcc\quicksort.php on line 10

PHP Quick Sort

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.