Php associated array sorting (quick sorting) causes well, I admit that I 've been working with quick sorting recently, various tests are writing quick sorting programs, and now I am using php to achieve quick sorting, different from the previous article, this php quick schedule can solve the actual needs. There is a situation where the environment and conditions are used, the associated arrays in php, if the following array data: php joined array sorting (fast sorting)
Cause
Okay, I admit that I 've been working on quick sorting recently. various tests are writing quick sorting programs. now I am using php to implement quick sorting, which is different from the previous article, this php quick schedule can solve the actual needs.
The environment and conditions are used to associate an array in php. if the following array data is as follows:
$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
Note that there is no pointer in php, so when you want to reference and pass, we cannot directly write quicksort (int * A, int begin, like C code, int end). Instead, you must use the & Operator of php to pass the address of the array and the quick sort function. in this way, you can implement reference transfer instead of value transfer in php.
Quick sort code
QuickSortProcess ($ array, 0, count ($ array)-1); print_r ($ array);/*** Description: obtain the position of the central point in the quick sorting */function QuickPartition (& $ array, $ left, $ right) {// 1. baseline definition $ stand = $ array [$ left]; // 2. scanning 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. get the central position $ array [$ left] = $ stand; return $ left;}/*** Description: main flow function of quick sorting */function QuickSortProcess (& $ array, $ begin, $ end) {// 1. variable definition $ begin = NULL; // The central point if ($ begin <$ end) {$ begin = QuickPartition ($ array, $ begin, $ end); QuickSortProcess ($ array, $ begin, $ sequence-1); QuickSortProcess ($ array, $ sequence + 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.
-
Dickeylth, 1st Floor, yesterday
-
For this problem, you can refer to the usort in php and write the custom sorting callback function. See http://www.php.net/manual/zh/function.usort.php
-
Re: zinss26914 yesterday
-
Reply to dickeylthn and check the link you provided. this is indeed the case and saves a lot of code. thanks a lot, haha. actually, I want to use my own sorting algorithm in my project, I always call the built-in functions of php, and I feel that I will not write the program anymore.