Review and summary of common PHP sorting algorithms

Source: Internet
Author: User
This is frequently used for sorting array elements in PHP. it is also available in previous projects. We use PHP native functions such as asortarsort for sorting, I didn't implement it myself, so I would like to summarize the several functions below. this will be constantly supplemented and I can also review and summarize them.

This is frequently used for sorting array elements in PHP. it is also available in previous projects. We also use PHP native functions such as asort arsort for sorting, I didn't implement it myself, so I would like to summarize the several functions below. this will be constantly supplemented and I can review and summarize them myself.

Go directly to the code!


$ Tmp) {// if the previous one is larger than the next one, it is from small to large $ arr [$ j + 1] = $ arr [$ j]; // Swap the small element with the previous one until it is moved to the appropriate position. move the next $ arr [$ j] = $ tmp; $ j --;}}} return $ arr;}/** select sort (one-dimensional array) * select the smallest (largest) element from the data element to be sorted, the sequence is placed at the end of the sorted series until all the data elements to be sorted are arranged. */Function selectSort ($ arr) {if (! Is_array ($ arr) | count ($ arr) = 0) {return $ arr;} $ count = count ($ arr); for ($ I = 0; $ I <$ count; $ I ++) {$ k = $ I; for ($ j = $ I + 1; $ j <$ count; $ j ++) {if ($ arr [$ k]> $ arr [$ j]) $ k = $ j; // Find the smallest if ($ k! = $ I) {$ tmp = $ arr [$ I]; $ arr [$ I] = $ arr [$ k]; $ arr [$ k] = $ tmp ;}}return $ arr ;}/ ** bubble sort (one-dimensional array) * compares the size of the elements to be sorted, it is found that the two data elements are exchanged in reverse order until there is no reverse order of data elements */function bubbleSort ($ array) {$ count = count ($ array ); if ($ count <= 0) {return false;} for ($ I = 0; $ I <$ count; $ I ++) {for ($ j = $ count-1; $ j> $ I; $ j --) {if ($ array [$ j] <$ array [$ j-1]) {// compare the number found for swap $ tmp = $ array [$ j]; $ array [$ j] = $ array [$ j-1]; $ array [$ j-1] = $ tmp; }}return $ array;}/** quick sort (one-dimensional array) **/function quickSort ($ array) {if (count ($ array) <= 1) {return $ array;} $ key = $ array [0]; $ left_arr = array (); $ right_arr = array (); for ($ I = 1; $ I $ Val) {$ arrKey [] = $ key; $ arrVal [] = $ val;} $ count = count ($ arrVal); if ($ count) {// Create an ordered array of keys for ($ key = 0; $ key <$ count; $ key ++) {$ arrKeyMap [$ key] = $ key ;} // sort the values for ($ I = 0; $ I <$ count; $ I ++) {for ($ j = $ count-1; $ j> $ I; $ j --){// <从小到大排列 升降在这修改                $bol ="$strOrder =='asc' ?$arrVal[$j]<$arrVal[$j-1]" :$arrval[$j]> $ ArrVal [$ j-1]; if ($ bol) {$ tmp = $ arrVal [$ j]; $ arrVal [$ j] = $ arrVal [$ j-1]; $ arrVal [$ j-1] = $ tmp; // bubble sorting of values, resulting in interaction with the array of keys $ keytmp = $ arrKeyMap [$ j]; $ arrKeyMap [$ j] = $ arrKeyMap [$ j-1]; $ arrKeyMap [$ j-1] = $ keytmp ;}} if (count ($ arrKeyMap )) {foreach ($ arrKeyMap as $ val) {$ arrReturn [] = $ arrKey [$ val] ;}} return $ arrReturn ;}} /*** use native functions to sort arrays by values */function arraySortByVal ($ arr, $ keys, $ type = 'asc ') {$ keysvalue = $ new_array = array (); foreach ($ arr as $ k => $ v) {$ keysvalue [$ k] = $ v [$ keys];} if ($ type = 'asc ') {asort ($ keysvalue);} else {arsort ($ keysvalue);} reset ($ keysvalue ); foreach ($ keysvalue as $ k => $ v) {$ new_array [$ k] = $ arr [$ k];} return $ new_array;


One of the following two methods for sorting array values is self-implemented by using the native PHP function, in fact, sorting is generally possible for data with a small amount of data on a single page. if sorting involves a large amount of data, we recommend that you integrate it into the basic MYSQL class.


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.