Php associated array sorting (fast sorting)

Source: Internet
Author: User


Use environment and conditions
In this case, the associated array in php is as follows:
[Php]
$ Array = array (
Array (
'Name' => "xiao ",
'Age' => 3
),
Array (
'Name' => 'wang ',
'Age' => 1
),
Array (
'Name' => 'chen ',
'Age' => 2
)
);

We need to sort the array's age fields. php's built-in functions, no matter which type of sort, obviously cannot meet our needs. Therefore, we can write a fast sorting code by ourselves, quickly implement our requirements

Notes
There is no pointer in php, so when we want to reference and pass, we cannot directly write quicksort (int * A, int begin, int end) like C code ), instead, we need to use the php & operator to pass the array address and the quick sorting function, so that we can implement reference transfer in php, instead of value transfer.

Quick Sort code
[Php]
QuickSortProcess ($ array, 0, count ($ array)-1 );
Print_r ($ array );
 
/**
* Description: gets the central point position in quick sorting.
*/
Function QuickPartition (& $ array, $ left, $ right ){
// 1. Benchmark Definition
$ Stand = $ array [$ left];

// 2. Scan from both ends of the interval until $ left = $ right.
While ($ left <$ right ){
While ($ left <$ right & $ array [$ right] ['age'] >=$ stand ['age']) {
$ Right --;
}
If ($ left <$ right ){
$ Array [$ left ++] = $ array [$ right];
}

While ($ left <$ right & $ array [$ left] ['age'] <= $ stand ['age']) {
$ Left ++;
}
If ($ left <$ right ){
$ Array [$ right --] = $ array [$ left];
}
}

// 3. Obtain the central point
$ Array [$ left] = $ stand;
Return $ left;
}
 
/**
* Description: main flow function for quick sorting
*/
Function QuickSortProcess (& $ array, $ begin, $ end ){
// 1. variable definition
$ Centers = NULL; // central point

If ($ begin <$ end ){
$ Partition = QuickPartition ($ array, $ begin, $ end );
QuickSortProcess ($ array, $ begin, $ sort-1 );
QuickSortProcess ($ array, $ rows + 1, $ end );
}
}

I used this fast sorting in the project. It was very happy. I did not waste the c code that took N days for the October 1 holiday to quickly sort the AC.

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.