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

Source: Internet
Author: User

Many people say that the algorithm is the core of the program, a program is better than poor, the key is the merits of the program algorithm. As a primary phper, although it is seldom exposed to algorithmic aspects of things. But for the bubble sort, insert sort, choose Sort, quick sort four basic algorithms, I think still have to master. Here are my own understanding of the four methods to analyze it again.
Requirements: Using bubble sort method, quick sorting method, selecting Sort method, inserting sorting method to sort the values in the following array in order from small to the next.
$arr (1,43,54,62,21,66,32,78,36,76,39);

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-1; $i + +)  {//The 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) {    //first determine if the need to continue    $length = count ($arr);    if ($length <= 1) {        return $arr;    }    If there is no return, the number of elements in the array is 1 extra, you need to sort    //Select a ruler    //Select the first element    $base _num = $arr [0];    Iterate through all elements except the ruler, put in two arrays according to the size relationship    //Initialize two arrays    $left _array = Array ();//$right less than the ruler    _array = Array ();//greater than the ruler For    ($i =1; $i < $length; $i + +) {        if ($base _num > $arr [$i]) {            //put on left array            $left _array[] = $arr [$i];        } else {            //Put right            $right _array[] = $arr [$i];        }    }    Then separate the left and right arrays with the same sort method    //Recursive call to this function, and record the result    $left _array = Quick_sort ($left _array);    $right _array = Quick_sort ($right _array);    Merge left ruler to the right of    return Array_merge ($left _array, Array ($base _num), $right _array);}

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

Related Article

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.