Fast sort & lt ;? Phpfunctionquicksort ($ str) {if (count ($ str) & lt; = 1) return $ str; // if the number is not big enough, directly return $ key = $ str [0]; // Obtain a value for comparison later. $ left_arr = array (); $ right_arr = array (); for ($ I = 1; $ I &
Fast sorting
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 ));
?>
Bubble sorting
Function bubbingSort (array $ array)
{
For ($ I = 0, $ len = count ($ array)-1; $ I <$ len; ++ $ I)
{
For ($ j = $ len; $ j> $ I; -- $ j)
{
If ($ array [$ j] <$ array [$ j-1])
{
$ Temp = $ array [$ j];
$ Array [$ j] = $ array [$ j-1];
$ Array [$ j-1] = $ temp;
}
}
}
Return $ array;
}
Print'
';
print_r(bubbingSort(array(1,4,22,5,7,6,9)));
print '
';