PHP four basic algorithms: bubbling, selection, insertion and quick Sort method

Source: Internet
Author: User

1. Bubble Sorting method
* Thinking Analysis: The law, as its name, is like bubbling, each time from the array to take a maximum number out.
* For example: 2,4,1//The First bubble that pops up is 4
* 2,1,4//The Second bubble that pops up is 2
* 1,2,4//The end is like this

$arr =array (1,43,54,62,21,66,32,78,36,76,39);  function Getpao ($arr) {    $len =count ($arr);  Set an empty array to receive the bubbling of bubbles  //That layer loop control needs to bubble the number of rounds for  ($i =1; $i < $len; $i + +)  {//This layer loop is used to control the number of times a number needs to be compared per round for    ($k = 0; $k < $len-$i, $k + +)    {       if ($arr [$k]> $arr [$k +1])        {            $tmp = $arr [$k +1];            $arr [$k +1]= $arr [$k];            $arr [$k]= $tmp;        }}}    


2. Select the sorting method:

Choose a sorting idea: Select a corresponding element at a time and place it in the specified position

function Select_sort ($arr) {//Realize dual loop completion, outer control wheel count, current minimum value. The number of comparisons of the inner control    //$i The current minimum position, the element to be involved in the comparison for    ($i =0, $len =count ($arr); $i < $len-1; $i + +) {        //First assume the location of the minimum value        $p = $i;        $j currently need to compare with which elements, $i behind.        for ($j = $i +1; $j < $len; $j + +) {            //$arr [$p] is the currently known minimum value            if ($arr [$p] > $arr [$j]) {     //compare, find smaller, Record the position of the minimum value, and at the next comparison, the//should be compared with a known minimum value.                $p = $j;            }        }        The location of the current minimum value has been determined and saved to $p. If the location of the minimum value is found to be different from the current assumed position $i, the position is interchangeable if        ($p! = $i) {            $tmp = $arr [$p];            $arr [$p] = $arr [$i];            $arr [$i] = $tmp;        }    }    Return the final result return    $arr;}

3. Insert Sort method

Insert Sort idea: Inserts the element that will be sorted into the specified position of the array that has assumed the sort number.
function Insert_sort ($arr) {    ///distinguish which part is already sorted/    /Which part is not sorted//    find one of the elements that needs to be sorted//    This element is starting with the second element, To the last element is the element that needs to be sorted    //Using a loop can be marked out    //i loop control every time the element needs to be inserted, once the element needs to be inserted control,    //indirectly has divided the array into 2 parts, subscript less than the current (left), is a sorted sequence for    ($i =1, $len =count ($arr); $i < $len; $i + +) {        //Get the element values that are currently being compared.        $tmp = $arr [$i];        Inner loop control compares and inserts        a for ($j = $i-1; $j >=0; $j-) {   //$arr [$i];//the element that needs to be inserted; $arr [$j];//the element to compare            if ($tmp < $ arr[$j]) {                //find the inserted element smaller, swap position                //swap the element behind with the previous element                $arr [$j +1] = $arr [$j];                Set the previous number to the number that currently needs to be swapped                $arr [$j] = $tmp;            } else {                ///If it encounters an element that does not need to be moved           //Because the array is already sorted, the preceding one does not need to be compared again. Break    ;    }}} Insert this element into a sequence that is already sorted.    //Return    $arr;}

4. Quick Sort Method

function Quick_sort ($arr) {
Determine if you need to proceed
$length = count ($arr);
if ($length <= 1) {
return $arr;
}
If there is no return, the number of elements in the array is 1 extra and needs to be sorted
Select a ruler
Select the first element
$base _num = $arr [0];
Iterates through all elements except the ruler, and fits into two arrays in relation to size
Initialize two arrays
$left _array = Array ();//less than the ruler
$right _array = Array ();//greater than the ruler
for ($i =1; $i < $length; $i + +) {
if ($base _num > $arr [$i]) {
Put in the left array
$left _array[] = $arr [$i];
} else {
Put it on the right
$right _array[] = $arr [$i];
}
}
The same sort method for the left and right arrays, respectively.
Call this function recursively and record the result
$left _array = Quick_sort ($left _array);
$right _array = Quick_sort ($right _array);
Merge left ruler right
Return Array_merge ($left _array, Array ($base _num), $right _array);

PHP four basic algorithms: bubbling, selection, insertion and quick Sort method

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.