PHP four basic algorithms: bubbling, selecting, inserting and fast sorting method ___ algorithm

Source: Internet
Author: User

1. Bubble Sort Method
* Thinking Analysis: The law, as its name, is like bubbling, each time from the array to take one of the largest number out.
* For example: 2,4,1//The first time the bubble is 4
* 2,1,4//The second time the bubble is 2
* 1,2,4//finally became 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 bubbles
  //The loop control needs to bubble up the number of rounds for
  ($i =1; $i < $len; $i + +)
  {//This layer loop is used to control the number of times per round that needs to be compared 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;
        }
  }} return $arr;

2. Select the sorting method:

Choice of sorting method: Each time you select a corresponding element, and then put it in the specified location

function Select_sort ($arr) {
//implementation of the dual loop completion, the outer control of the number of wheels, the current minimum value. The number of comparisons of inner control
    //$i The current minimum position, the element for which you want to participate in the comparison for
    ($i =0, $len =count ($arr) $i < $len-1; $i + +) {
        //Assume the position of the lowest value first
        $p = $i;
        $j are currently required 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]) {
     //comparison, found smaller, Record the position of the minimum value, and
 //should be compared with the known minimum value at the next comparison.
                $p = $j;
            }
        }
        The position of the current minimum value has been determined and saved to the $p.
 //If the location of the minimum value is found to be different from the current assumed position $i, the position interchange can be if
        ($p!= $i) {
            $tmp = $arr [$p];
            $arr [$p] = $arr [$i];
            $arr [$i] = $tmp;
        }
    }
    Returns the final result return
    $arr;
}

3. Insert Sort method

Insert Sort idea: Inserts the element to be sorted into the specified position of an array that has been assumed to be a sort number.
function Insert_sort ($arr) {//distinguish which part is already sorted/////find one of the elements that needs to be sorted//The element is
    starting with the second element. To the last element is this element that needs to be sorted
    //Use the loop to mark out the
    //i loop controls each element that needs to be inserted, once the element that needs to be inserted is controlled,
    //indirectly, the array is divided into 2 parts, the subscript is less than the current (left), is a sorted sequence for
    ($i =1, $len =count ($arr), $i < $len, $i + +) {
        //Get the current element value to compare.
        $tmp = $arr [$i];
        The inner loop controls the comparison and inserts
        a for ($j = $i-1; $j >=0; $j-) {
   //$arr [$i];//the element to be inserted; $arr [$j] the element to be compared
            if ($tmp < $ arr[$j]) {
                //Discover the inserted element to be small, swap position
                //swap the element behind with the preceding element
                $arr [$j +1] = $arr [$j];
                Set the previous number to the number of current needs to be exchanged
                $arr [$j] = $tmp;
            } else {/
                /If you encounter an element that does not need to be moved
           //because it is already sorted to be an array, the previous one does not need to be compared again. Break
                ;
    }
    }} Inserts this element into the sorted sequence.
    //Back to return
    $arr;
}

4. Quick Sort Method

function quick_sort ($arr) {//First determine whether the need to continue $length = count ($arr);
    if ($length <= 1) {return $arr;
    //If not returned, the number of elements in the array is more than 1, you need to sort//Select a ruler//select the first element $base _num = $arr [0]; Iterate over all elements except the ruler, put into two arrays by size relationship//Initialize two arrays $left _array = Array (), or less than the ruler's $right _array = Array ();//greater than the ruler's for ($ I=1; $i < $length;
        $i + +) {if ($base _num > $arr [$i]) {//put array to the left $left _array[] = $arr [$i];
        else {//Put the right $right _array[] = $arr [$i];
    }//And then to the left and right of the array to do the same sort of processing/recursive call this function, and record the result $left _array = Quick_sort ($left _array);
    $right _array = Quick_sort ($right _array);
Merges the left ruler right return Array_merge ($left _array, Array ($base _num), $right _array); }

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.