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 release can solve the actual needs. in the following article, I will use the C language to propose two solutions for fast PHP sorting:
- Achieve quick sorting by yourself, and use the idea of divide and merge
- Call the usort of the system, similar to the qsort of C. Thanks for the reminder of dickeylth.
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. The simple sort function provided by PHP obviously cannot meet our needs. Therefore, we can write a fast sorting code by ourselves, quickly implement our requirements. Note that no pointer exists in PHP. Therefore, when we want to reference and transmit data, we cannot directly write quicksort (int *, int begin, int end). Instead, use the & operator of PHP to pass the address of the array to the quick sort function, in this way, you can implement reference transfer in PHP, instead of value transfer, fast sorting code grouping + merge to achieve fast sorting
/*** Description: obtain the location of the central vertex ** @ Param array $ array * @ Param int $ left * @ Param int $ right * @ Param string $ field * @ return int */function fetcharrayray( & $ array, $ left, $ right, $ field) {// baseline Definition $ stand = $ array [$ left]; // traverses the array while ($ left <$ right) {While ($ left <$ Right & $ array [$ right] [$ field] >=$ stand [$ field]) {$ right --;} if ($ left <$ right) {$ array [$ left ++] = $ array [$ right];} while ($ left <$ Right & $ array [$ left] [$ field] <= $ stand [$ field]) {$ left ++ ;} if ($ left <$ right) {$ array [$ right --] = $ array [$ left];} // obtain the central position $ array [$ left] = $ stand; return $ left;}/*** description: quick sorting main program ** @ Param array $ array * @ Param int $ begin * @ Param int $ end * @ Param string $ field */function quicksort (& $ array, $ begin, $ end, $ field) {// variable definition $ begin = NULL; if ($ begin <$ end) {$ begin = fetcharrayray( $ array, $ begin, $ end, $ field); quicksort ($ array, $ begin, $ fields-1, $ field); quicksort ($ array, $ struct + 1, $ end, $ field );}}
Call example
<? PHP $ array = array (Array ('name' => "Xiao", 'age' => 3), array ('name' => 'wang ', 'age' => 1), array ('name' => 'chen', 'age' => 2), array ('name' => 'zhengyi ', 'age' => 4), array ('name' => 'chai', 'age' => 14); $ begin = gettime (); quicksort ($ array, 0, count ($ array)-1, 'age'); print_r ($ array); $ end = gettime (); $ spend = $ end-$ begin; echo "time spent :". $ spend. "seconds \ n"; function gettime () {list ($ msec, $ Sec) = explode ("", microtime (); Return (float) $ msec + (float) $ sec ;}
The usort of the system for calling results is similar to calling qsort in C. You need to write the comparison function yourself, which is similar to that in C.
/*** Description: similar to the qsort comparison function in C ** @ Param array $ A * @ Param array $ B * @ Param string $ field * @ return int */function compare ($, $ B) {return $ A ['age']-$ B ['age'];} usort ($ array, "Compare ");
Call example
<? PHP $ array = array (Array ('name' => "Xiao", 'age' => 3), array ('name' => 'wang ', 'age' => 1), array ('name' => 'chen', 'age' => 2), array ('name' => 'zhengyi ', 'age' => 4), array ('name' => 'chai', 'age' => 14); $ begin = gettime (); usort ($ array, "Compare"); print_r ($ array); $ end = gettime (); $ spend = $ end-$ begin; echo "time spent :". $ spend. "seconds \ n"; function gettime () {list ($ msec, $ Sec) = explode ("", microtime (); Return (float) $ msec + (float) $ sec ;}
Effect