PHP sorting algorithm review and summary

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 continue to go directly to the code!
The code is as follows:
/*
* Insert sorting (one-dimensional array)
* Each time, a data element to be sorted is inserted to an appropriate position in the sorted sequence to make the sequence still orderly until all the data elements to be sorted are inserted.
*/
Function insertSort ($ arr ){
If (! Is_array ($ arr) | count ($ arr) = 0 ){
Return $ arr;
}
$ Count = count ($ arr );
For ($ I = 1; $ I <$ count; $ I ++ ){
If (isset ($ arr [$ I]) {
$ Tmp = $ arr [$ I]; // obtain the value of the last element.
$ J = $ I-1; // Obtain the subscript
While ($ arr [$ j]> $ tmp) {// if the first one is larger than the last one, it is from small to large.
$ Arr [$ j + 1] = $ arr [$ j]; // Swap a small element with the previous one until it is moved to a proper position and move the next one
$ Arr [$ j] = $ tmp;
$ J --;
}
}
}
Return $ arr;
}
  
/*
* Select sort (one-dimensional array)
* Select the smallest (largest) element from the data elements to be sorted, and place the elements in sequence 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 sorting (one-dimensional array)
* Compare the size of the elements to be sorted. if the two data elements are in the opposite order, they are exchanged until there is no data element in the reverse order.
*/
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 of found values for exchange
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ j-1];
$ Array [$ j-1] = $ tmp;
}
}
}
Return $ array;
}
  
/*
* Fast sorting (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 If ($ array [$ I] <= $ key ){
$ Left_arr [] = $ array [$ I];
} Else {
$ Right_arr [] = $ array [$ I];
}
}
$ Left_arr = quickSort ($ left_arr );
$ Right_arr = quickSort ($ right_arr );
Return array_merge ($ left_arr, array ($ key), $ right_arr );
}
/**
* Sort by element value
* StrOrder is the sort order asc ascending desc descending order
*/
Function sortByVal ($ arr, $ strOrder = 'asc ')
{
If (! Is_array ($ arr) | count ($ arr) = 0)
{
Return $ arr;
}
$ ArrReturn = array ();
Foreach ($ arr as $ key => $ 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 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 value
*/
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.