PHP implementation of common sorting algorithm summary _php skills

Source: Internet
Author: User
Tags array length

This paper summarizes the common PHP sorting algorithm, which has good reference value in the design of the algorithm. We are now sharing them for your reference. Specifically as follows:

First, insert sort

A simple description of text, such as $arr = Array (4,2,4,6,3,6,1,7,9); Such a set of numbers is sorted sequentially:
So, first, take the second element of the array and the first element, and if the first element is greater than the second element, then let the two positions be interchanged, and then take the third element of the array, and then the second, the first element, and the third element, if it is small, then swap. by analogy. This is the insertion sort, and its time frequency is: 1+2+...+ (n-1) = (n^2)/2. Then its time complexity is O (n^2).

The PHP implementation code is as follows:

<?php
function Insertsort ($arr) {
   $count = count ($arr);
   if ($count <2) {return
  $arr; 
   }
   for ($i =1; $i < $count; $i + +) {
   $tmp = $arr [$i];
   $j = $i-1;
   while (j>=0&& $arr [$j]< $arr [$i]) {
  $arr [$i] = $arr [$j];           
  $arr [$j] = $tmp;
  $j--;
   }
    }
    return $arr;
>

Second, select the sort

Select a sort to describe words in words, such as: $arr = Array (4,3,5,2,1);

First, take the first and all the back, find the smallest number, then swap it with the first array (of course, if it's the first smallest, then do not swap), then loop, i.e., take the second and subsequent comparisons, find the smallest number, then swap it with the second number, and so on. That means finding the smallest remaining value each time. Can be obtained: the first time, the frequency is N, (the first and the back of the n-1 comparison, find the smallest, and then see whether the first, not the first words to exchange) in the future, in turn is minus one. Its time complexity, also O (n^2);

The PHP implementation code is as follows:

<?php
function Selectsort ($arr) {

   $count = count ($arr);
   if ($count <2) {return
  $arr; 
   }
   for ($i =0; $i < $count; $i + +) {
   $min = $i;
   For (j= $i +1; $j < $count; $j + +) {
  if ($arr [$min]> $arr [$j]) {
    $min = $j;////Find the lower subscript of the smallest element
  }
   }
   if ($min!= $i) {//if subscript is not $i then interchange.
   $tmp = $arr [$i];           
    $arr [$i] = $arr [$min];
    $arr [$min] = $tmp;
    }
    }
    return $arr;
>

Third, bubble sort

Bubble sort is actually compared to the choice of the sort, there is no significant difference. is to find the smallest, put to the left end. Loop through the problems in turn. The difference is that the number of swap places for bubble sort is more frequent, while the selection order is to find the lowest element subscript, and then the direct and leftmost swap position.


The PHP implementation code is as follows:

<?php
function Selectsort ($arr) {

   $count = count ($arr);
   if ($count <2) {return
  $arr; 
   }
   For ($i =0 $i < $count; $i + +) {for
   (j= $i +1; $j < $count; $j + +) {
  if ($arr [$i]> $arr [$j]) {
    $tmp = $arr [$i] ];           
    $arr [$i] = $arr [$i];
    $arr [$i] = $tmp;
  }
    }} return $arr;
>

Four, quick sort

Quick sort, in terms of words, select a value $a from the array, then compare it to the rest of the elements, and place it in the right of the array, rather than the $a, and put it in the left of the array instead. The left right is then recursively called, that is, the left right is subdivided, and the array is finally merged.

Quick sort of PHP implementation:

<?php
function Mysort ($arr) {

   $count = count ($arr);
   if ($count <2) {return
  $arr; 
   }
   $key = $arr [0];//selects the first element as the comparison element, optionally the other
    $left = Array ();       
    $right = Array ();
    For ($i =1 $i < $count $i + +) {
   if ($key >= $arr [$i]) {
  $left [] = $arr [$i]; 
   } else{
  $right [] = $arr [$i];
    }
    }
    $left = Mysort ($left);
    $right = Mysort ($right);
    $result = Array_merge ($left, $right);
    return $result;
>

Five, merge sort

In fact, merging sort is a kind of splitting, merging idea. And quick sort ideas have in common, a bunch on the left, a bunch on the right, and then merge. Sort by recursive implementation.  What's the difference? Their difference is also the ideological nature of the difference, a quick sort of split, is to select a specific value for size comparison, which is divided into left and right. That is, a small pile of left, a large pile into the right. Then, the small left again subdivided into left1 right1 .... Sort by doing similar recursion. In other words, the left1 at the end of recursion is the smallest value.

The merge sort, which is split from left to right, has been recursively divided into 2 or 1 of the smallest granularity of an array, before starting to compare the size and then merging. The comparison is as follows: The element left by the son is compared to the right element of the son, and then sorted and merged into a father left or right. Here, until you get the respective sort merge complete the last two arrays: the most original left and right, and only until their respective order, cannot confirm the order of the entire array, or need to be combined with the end to complete the true meaning of the sort.

<?php
function Gbsort ($arr) {
    if (count ($arr) <=1) {return $arr;}
    $min = Floor (count ($arr)/2);//Take the middle number to split
    $left = Array_slice ($arr, 0, $min);
    $right = Array_slice ($arr, $min);
    $left = Gbsort ($left); Recursive
    $right = Gbsort ($right);
    Return Get_merge ($left, $right);//Call the sort merge function to merge
} function
Get_merge ($left, $right) {while
    count ($ left) && count ($right)) {
        $m [] = $left [0]> $right [0]? Array_shift ($right): Array_shift ($left);
        For comparison, small removal, and put into the array $m.
    }
    Return Arr_merge ($m, $left, $right);//merge (because you do not know which of the left will be empty, make a unified merge)
}

?>

Vi. Heap Sequencing

In this example, the Fixdown function implements a downward adjustment of a node, where the default is the starting node of 1, to facilitate the calculation of parent-child node relationships

Note:

Parent-child relationship with starting node 1: Parental node K, child node is 2K, 2k+1 child node J, parent node is floor (J/2) floor to rounding down
Parent-child relationship with starting node 0: Parental node K, child node 2k+1, 2k+2 child node J, parent node floor ((j-1)/2)

The parameter $k is the adjustment point position, $lenth the array length, which is the coordinates from 1 to the last node.

<?php
function Fixdown (& $arr, $k, $lenth)
{while
(2* $k <= $lenth) {//If the current node has child nodes, you need to continue the loop
    $ j = $k *2;
    if ($j < $lenth && $arr [$j]< $arr [$j +1]) $j + +;  As long as the child node has the right node, and the right node is larger than the left node, then switch to the right node operation.
    if ($arr [$j] < $arr [$k]) break;//If the child nodes do not have a parent node, the adjustment ends.
    Exch ($arr [$j], $arr [$k]);  $k = $j;
  }
}

Function exch (& $a, & $b) {
  $tmp = $a; $a = $b; $b = $tmp;
}

Function Headsort (& $arr)
{
  $len = count ($arr);
  Array_unshift ($arr, NULL);
  for ($i = $len/2; $i >=1; $i-) {
    Fixdown ($arr, $i, $len);
  while ($len >1) {
    exch ($arr [1], $arr [$len]);
    Fixdown ($arr, 1,--$len);
  }
  Array_shift ($arr);
$arr = Array (4,6,4,9,2,3);
Headsort ($arr);
? >   

It is hoped that the example of the sorting algorithm in this paper will be helpful to the PHP program design.

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.