Take a value and compare it with other values. put a small value on the left of the value, a large value on the right of the value, and then follow this method to recursively code 1:
The code is as follows:
Function quicksort ($ str ){
If (count ($ str) <= 1) return $ str; // if the number is not big, return directly
$ Key = $ str [0]; // Obtain a value for comparison later;
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I If ($ str [$ I] <= $ key)
$ Left_arr [] = $ str [$ I];
Else
$ Right_arr [] = $ str [$ I];
}
$ Left_arr = quicksort ($ left_arr); // recursion;
$ Right_arr = quicksort ($ right_arr );
Return array_merge ($ left_arr, array ($ key), $ right_arr); // combines the left and right values into an array;
} // The following is the test
$ Str = array (, 0 );
Print_r (quicksort ($ str ));
?>
Code 2:
The code is as follows:
/* @ Quick sorting */
Function quickSort ($ left, $ right, $ arr ){
$ L = $ left;
$ R = $ right;
$ Align = $ arr [($ left + $ right)/2];
$ Temp = 0;
While ($ l <$ r ){
While ($ arr [$ l] <$ scheme ){
$ L ++;
}
While ($ arr [$ r]> $ region ){
$ R --;
}
If ($ l >=$ r) break;
$ Temp = $ arr [$ l];
$ Arr [$ l] = $ arr [$ r];
$ Arr [$ r] = $ temp;
If ($ arr [$ l] === pivot) -- $ r;
If ($ arr [$ r] === pivot) ++ $ l;
}
If ($ l = $ r ){
$ L ++;
$ R --;
}
If ($ left <$ r ){
QuickSort ($ left, $ r, $ arr );
} Elseif ($ right> $ l ){
QuickSort ($ l, $ right, $ arr );
} Else {
Return $ arr;
}
}