PHP Sorting algorithm Implementation explained

Source: Internet
Author: User
Tags benchmark

The so-called ordering is to make a series of records, according to the size of one or some of the keywords, increment or decrement the arrangement of operations. The sorting algorithm is how to make the records arranged according to the requirements of the method. Sorting algorithms are given considerable attention in many areas, especially in the processing of large amounts of data. A good algorithm can save a lot of resources. Considering the various limitations and specifications of data in various fields, it is a great deal of reasoning and analysis to get a good algorithm that accords with the actual.

The values in the following array are sorted from small to large by using the insertion sort method, the bubble sort method, the sorting method, and the fast sorting method.

$arr (12,43,57,32,51,76,36,91,28,46,40);


1. Insert Sort method

Analysis: The pre-determined numbers are already in order, and now you want to insert the nth number into an ordered array, so that the n numbers are placed in order, so that they are looped back and forth until they are all sorted.

The specific code is implemented as follows:

$arr (12,43,57,32,51,76,36,91,28,46,40); Function insertsort ($arr)  {     $len =count ($arr)      for ($i =1,  $i < $len;  $i +)  {          $tmp  =  $arr [$i];        for ($j = $i-1; $j >=0; $j-)  {            if ($tmp  <   $arr [$j])  {                 //Compare size when the digital hour Exchange position, the following numbers are exchanged with the previous numbers                   $arr [$j +1] =  $arr [$j];                  $arr [$j] =  $tmp;             } else {                 //do not need to, skip directly                  break;            }         }    }    return  $arr;}


2. Bubble Sort Method

Analysis: From the back to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, so that the smaller numbers upward, that is, each adjacent to the number of the comparison of the order, the sequence does not match when the position.

The specific code is implemented as follows:

$arr (12,43,57,32,51,76,36,91,28,46,40), function Bubblesort ($arr) {$len =count ($arr);            for ($i =1; $i < $len; $i + +) {//Cycle comparison 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;}


3. Select the Sorting method

Analysis: The smallest number is selected and the first position is exchanged, then the remaining number is again found the smallest number and the second position exchange, in this cycle to the penultimate number and the last number before the end of the comparison.

The specific code is implemented as follows:

$arr (12,43,57,32,51,76,36,91,28,46,40), Function selectsort ($arr)  {  $len =count ($arr);     for ($i =0;  $i < $len -1;  $i +)  {         //assume the location of the minimum value          $p  =  $i;         for ($j = $i +1;  $j < $len;  $j +)  {             //$arr [$p]  known minimum value              if ($arr [$p] >  $arr [$j])  {        The      //comparison finds a smaller record of the location of the minimum value, and compares it with a known minimum value at the next comparison.                  $p  =   $J;            }         }        //determines the currentThe position of the decimal value is saved in the $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  $arr;}


4. Fast sorting method

Analysis: By a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

The specific code is implemented as follows:

$arr (12,43,57,32,51,76,36,91,28,46,40); Function quicksort ($arr)  {    //decide whether to proceed      $length  = count ($arr);     if ($length  <= 1)  {        return  $arr;    }     //Select the first number as a benchmark      $base _num =  $arr [0];    // Iterates through all numbers except the Datum, puts in two arrays according to the size relationship, then initializes two arrays      $left _array = array ();   //is less than the baseline      $right _array = array ();   //is greater than the benchmark     for ($i = 1;   $i < $length;  $i + +)  {        if ($base _num >   $arr [$i])  {            //into the left array               $left _array[] =  $arr [$i];         } else {            // Put the right array              $right _array[] =  $arr [ $i];        }    }    // The same sort method for two arrays recursively      $left _array = quick_sort ($left _array);      $right _array = quick_sort ($right _array)     //merged arrays      Return array_merge ($left _array, array ($base _num),  $right _array);}


PHP Sorting algorithm Implementation explained

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.