Summary of common sorting algorithms implemented by php

Source: Internet
Author: User
This article mainly introduces the summary of common sorting algorithms implemented by php, including Insertion sorting, selection sorting, bubble sorting, fast sorting, merge sorting, and heap sorting, with a detailed description of the sorting algorithm, you can refer to this article to summarize common php sorting algorithms, which have good reference value for algorithm design. I will share it with you for your reference. The details are as follows:

I. Insert sorting

Use a simple description in text, such as $ arr = array (,); to sort a group of numbers in sequence:
So, first, compare the second element of the array with the first element. if the first element is greater than the second element, the positions of the two are interchangeable. Next, take the third element of the array, compare the first element with the second one. if the third element is small, it is interchangeable. And so on. This is the insertion sorting. the time frequency is: 1 + 2 +... + (n-1) = (n ^ 2)/2. The time complexity is O (n ^ 2 ).

The php implementation code is as follows:

<?phpfunction 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;  }?>

II. select sorting

If you select the language description for sorting, you can do this, for example: $ arr = array (, 1 );

First, take the first and all the following ratios, find the smallest number, and then swap it with the first array (of course, if it is the first smallest, then there is no need to swap it ), next, take the second and subsequent comparisons to find the smallest number, and then swap it with the second number, and so on, that is, find the remaining smallest value each time. It can be obtained: for the first time, the time frequency is n. (The first is compared with the n-1 in the end. find the minimum value and see if it is the first one. if it is not the first one, swap it, the order is minus one. Its time complexity is also O (n ^ 2 );

The php implementation code is as follows:

<? Phpfunction 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 subscript of the smallest element} if ($ min! = $ I) {// it is interchangeable if the subscript is not $ I. $ Tmp = $ arr [$ I]; $ arr [$ I] = $ arr [$ min]; $ arr [$ min] = $ tmp ;}} return $ arr ;}?>

III. Bubble sorting

In fact, bubble sorting is not significantly different from the selected sorting. To the leftmost end. Solve the problem cyclically. The difference is that bubble sorting has a large number of switching locations, while selecting sorting is to find the subscript of the smallest element, and then directly exchange the position with the leftmost end.


The php implementation code is as follows:

<?phpfunction 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;  }?>

IV. quick sorting

In quick sorting, select a value $ a from the array and compare it with other elements. values larger than $ a are placed in the array right. otherwise, put it in the left array. Then, recursively call left right, namely, subdivide left right, and merge the array.

Php for fast sorting:

<? Phpfunction mySort ($ arr) {$ count = count ($ arr); if ($ count <2) {return $ arr ;}$ key = $ arr [0]; // select the first element as the comparison element. optional values: $ 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 ;}?>

V. merge sorting

In fact, merge sorting is a splitting and merging idea. It has something in common with the fast sorting idea: a pile on the left, a pile on the right, and then merge. Implement recursive sorting. What is the difference? Their difference is also the essential difference in thinking. the sharding of fast sorting is to compare the size of a specific value, which is divided into left and right. That is, put a small pile into left, and put a large pile into right. Then, the small left is subdivided into left1 right1 .... Perform similar recursion to complete sorting. That is to say, the left1 at the end of recursion is the minimum value.

Merge sorting is to split from the left and right sides of the Ry and recursively split it into the minimum granularity array of 2 or 1 before comparison and merge. The size of the comparison here is: the child left element is compared with the son's right element, and then sorted and merged into the father's left or right. Here, the last two arrays are merged until the original left and right are obtained, and the Order of the entire array is not confirmed, it is still necessary to merge after the final left right comparison to complete the sorting in the true sense.

<? Phpfunction gbSort ($ arr) {if (count ($ arr) <= 1) {return $ arr ;}$ min = floor (count ($ arr)/2 ); // split the data with an intermediate number $ left = array_slice ($ arr, 0, $ min); $ right = array_slice ($ arr, $ min ); $ left = gbSort ($ left); // recursion $ 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 in the array $ m. } Return arr_merge ($ m, $ left, $ right); // merge (because you do not know which of the left right statements is null, merge them in a unified manner)}?>

6. heap sorting

In this example, the fixDown function is used to adjust down a node. the default value is 1, which facilitates the calculation of parent-child node relationships.

Note:

Parent-child relationship with the start node 1: parent node k, child node is 2 K, 2 k + 1 child node j, parent node is floor (j/2) floor is rounded down
Parent-child relationship with the start node 0: parent node k, child node 2 K + 1, 2 k + 2 child node j, parent node is floor (j-1)/2)

The parameter $ k is used to adjust the vertex position, and $ lenth is the array length, that is, the coordinates from the start of 1 to the last node.

<? Phpfunction fixDown (& $ arr, $ k, $ lenth) {while (2 * $ k <= $ lenth) {// as long as the current node has a subnode, you need to continue this loop $ j = $ k * 2; if ($ j <$ lenth & $ arr [$ j] <$ arr [$ j + 1]) $ j ++; // if the child node has a right node and the right node is larger than the left node, switch to the right node. If ($ arr [$ j] <$ arr [$ k]) break; // if no parent node is large, 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 (,); headSort ($ arr);?>

I hope the sorting algorithm examples described in this article will be helpful for php programming.

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.