Quick Sort is an improvement of Bubble sorting, an unstable sorting algorithm proposed by Tony Hall in 1962. Basic steps of quick sorting: pick out an element from a series (usually referred to as a "benchmark"), and split the data to be sorted into two separate parts by one click, all the data in one part is smaller than the benchmark, and all the data in the other part is larger than the benchmark. The data that is equal to the benchmark can be placed in either part of the two parts, then, follow this method to recursively process the two data parts separately to convert the entire data into an ordered sequence. Fast sorting. In the best case, the time complexity is O (nlogn); in the worst case, the time complexity is O (n2); and the average time complexity is O (nlogn ).
Quick sorting example:
PHP-based quick Sorting Algorithm
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
<?php/** * Data structures and algorithms (implemented in PHP)-quick sort ). *
* @ Author creative programming (topphp. org) * @ Copyright (c) 2013 creative programming (topphp. org) All Rights Reserved * @license http://www.opensource.org/licenses/mit-license.php MIT LICENSE * @version 1.0.0 - Build20130614 */class
QuickSort { /** * Array of data to be sorted. *
* @var array */ private
$data; /** * The length of the data array. *
* @var integer */ private
$size; /** * Whether the data array is sorted. *
* @var boolean */ private
$done; /** * Constructor-Initialize data. *
* @ Param array $ array of data to be sorted. */ public
function __construct(array
$data) { $this->data =
$data; $this->size =
count($this->data); $this->done = FALSE; } /** * Swap the positions of two elements in the data array. *
* @ Param integer $ X index of the element in the array. * @ Param integer $ y index of the element in the array. */ private
function swap($x,
$y) { $temp
= $this->data[$x]; $this->data[$x] =
$this->data[$y]; $this->data[$y] =
$temp; } /** * Fast sorting. *
* @ Param integer $ left index of the first element in the array. * @ Param integer $ right index of the last element in the array. */ private
function sort($left,
$right) { if
($left
>= $right) { return
; } $pivot
= $this->data[$left]; $start
= $left; $end
= $right; while
($start
< $end) { while
($start
< $end &&
$this->data[$end] >=
$pivot) { --$end; } $this->swap($start,
$end); while
($start
< $end &&
$this->data[$start] <
$pivot) { ++$start; } $this->swap($start,
$end); } $this->sort($left,
$start - 1); $this->sort($start
+ 1, $right); } /** * Obtain the sorted data array. *
* @ Return array returns the sorted data array. */ public
function getResult() { if
($this->done) { return
$this->data; } $this->sort(0,
$this->size - 1); $this->done = TRUE; return
$this->data; }}?> |
Sample Code
| 1234 |
<?php$quick
= new QuickSort(array(5, 3, 8, 9, 6, 2));echo
'<pre>', print_r($quick->getResult(), TRUE),
'</pre>';?> |