Copy codeThe Code is as follows:
<? Php
/**
* Quick sort
*
**/
Function sort_quick ($ arrData ){
If (empty ($ arrData) |! Is_array ($ arrData) return false;
$ Flag = $ arrData [0];
$ Len = count ($ arrData)-1;
If ($ len = 0) return $ arrData; // if only one data array is returned directly
$ ArrLeft = array ();
$ ArrRight = array ();
$ Len_l = 0;
$ Len_r = 0;
For ($ I = 1; $ I <= $ len; $ I ++ ){
If ($ arrData [$ I] <$ flag ){
$ ArrLeft [$ len_l] = $ arrData [$ I]; // left
$ Len_l ++;
} Else {
$ ArrRight [$ len_r] = $ arrData [$ I]; // Add a value greater than or equal to the right
$ Len_r ++;
}
}
// Merge Arrays
$ ArrResult = array ();
If ($ len_l ){
$ ArrLeft = sort_quick ($ arrLeft );
For ($ I = 0; $ I <= $ len_l-1; $ I ++ ){
$ ArrResult [$ I] = $ arrLeft [$ I];
}
}
$ ArrResult [$ len_l] = $ flag;
$ Len_l ++;
If ($ len_r ){
$ ArrRight = sort_quick ($ arrRight );
For ($ I = 0; $ I <= $ len_r-1; $ I ++ ){
$ ArrResult [$ len_l] = $ arrRight [$ I];
$ Len_l ++;
}
}
Echo "=", $ flag, "============================================== ===< br/> ";
Echo "data:", print_r ($ arrData), "<br/> ";
Echo "filter left:", print_r ($ arrLeft), "<br/> ";
Echo "filter right:", print_r ($ arrRight), "<br/> ";
Echo "return:", print_r ($ arrResult), "<br/> ";
Return $ arrResult;
}
// $ List = array );
$ List = array (, 33, 50, 3, 67 );
$ List = sort_quick ($ list );
Echo "<pre>"; print_r ($ list );