A quick sort, bubble sort _php instance of the sort algorithm for PHP version

Source: Internet
Author: User

First, quick sort

1. Introduction
Fast sequencing is a sort algorithm developed by Donny Holl. In average, sort n items to be 0 (n log n) times compared. In the worst-case scenario, 0 (N2) comparisons are required, but this is not a common situation. In fact, fast sorting is typically faster than the other 0 (n log n) algorithms because its internal loops (inner Loop) can be implemented efficiently on most architectures.
The quick sort uses the divide-and-conquer method (Divide and conquer) policy to divide a serial (list) into two sub serial (sub-lists).
2. Steps
Pick an element from a series, called a "datum" (pivot),
Reorder the series, all elements smaller than the base value placed in front of the datum, all elements are larger than the base value behind the datum (the same number can be on either side). After the partition exits, the datum is positioned in the middle of the series. This is called a partition (partition) operation.
recursively (recursive) sorts the substrings that are less than the datum elements and those that are larger than the datum values.
3. Code implementation

Copy Code code as follows:
function QuickSort (array $array)
{
$len = count ($array);
if ($len <= 1)
{
return $array;
}
$key = $array [0];
$left = Array ();
$right = Array ();
for ($i =1; $i < $len; + + $i)
{
if ($array [$i] < $key)
{
$left [] = $array [$i];
}
Else
{
$right [] = $array [$i];
}
}
$left = QuickSort ($left);
$right = QuickSort ($right);
Return Array_merge ($left, Array ($key), $right);
}

print ' <pre> ';
Print_r (QuickSort) (Array (1,4,22,5,7,6,9));
print ' </pre> ';


4. Sorting effect

The process of sorting a column of numbers by using the Quick Sort method



Second, bubble sort

1. Introduction
Bubble sort (Bubble sort, Taiwan translation: bubble sort or bubble sort) is a simple sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence.
2. Steps
Compare the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same work for each pair of adjacent elements, from the first pair to the end of the last couple. At this point, the final element should be the largest number.
Repeat the above steps for all elements except the last one.
Continue to repeat the previous steps for less and fewer elements until no pair of digits need to be compared.
3. Code implementation

Copy Code code as follows:
<?php
function Bubbingsort (array $array)
{
For ($i =0, $len =count ($array)-1; $i < $len; + + $i)
{
for ($j = $len; $j > $i;--$j)
{
if ($array [$j] < $array [$j-1])
{
$temp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $temp;
}
}
}
return $array;
}

print ' <pre> ';
Print_r (Bubbingsort) (Array (1,4,22,5,7,6,9));
print ' </pre> ';


4. Sorting process

The process of using bubble sort to sort a column of numbers

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.