Php instance code: Various sorting algorithms. Php instance code: Various sorting algorithms bubble sorting functionmaopao_sort ($ demo) {$ numcount ($ demo); for ($ i0; $ I $ num; $ I ++) {for ($ j $ num-1; $ j $ I; $ j --) {if ($ demo [$ j] $ demo [$ j-1]) {php instance code: sorting Algorithms
// Bubble sort
Function maopao_sort ($ demo ){
$ Num = count ($ demo );
For ($ I = 0; $ I <$ num; $ I ++ ){
For ($ j = $ num-1; $ j> $ I; $ j --){
If ($ demo [$ j] <$ demo [$ j-1]) {
$ Temp = $ demo [$ j];
$ Demo [$ j] = $ demo [$ j-1];
$ Demo [$ j-1] = $ temp;
}
}
}
Return $ demo;
}
// Insert sorting
Function charu_sort ($ demo ){
$ Num = count ($ demo );
For ($ I = 1; $ I <$ num; $ I ++ ){
$ Temp = $ demo [$ I];
$ Dqweizhi = $ I-1; // record the current location
While ($ dqweizhi> = 0) & ($ temp <$ demo [$ dqweizhi]) {
$ Demo [$ dqweizhi + 1] = $ demo [$ dqweizhi];
$ Dqweizhi --;
}
$ Demo [$ dqweizhi + 1] = $ temp;
}
Return $ demo;
}
// Sort by selection
Function select_sort ($ demo ){
$ Num = count ($ demo );
For ($ I = 0; $ I <$ num-1; $ I ++ ){
$ Temp = $ demo [$ I];
$ Dqweizhi = $ I;
For ($ j = $ I + 1; $ j <$ num; $ j ++ ){
If ($ demo [$ j] <$ temp ){
$ Temp = $ demo [$ j];
$ Dqweizhi = $ j;
}
}
$ Demo [$ dqweizhi] = $ demo [$ I];
$ Demo [$ I] = $ temp;
}
Return $ demo;
}
// Quick sorting
Function quick_sort ($ demo)
{
$ Num = count ($ demo );
If ($ num <= 1 ){
Return $ demo;
}
$ Key = $ demo [0];
$ Left_array = array ();
$ Right_array = array ();
For ($ I = 1; $ I <$ num; $ I ++ ){
If ($ demo [$ I] <= $ key ){
$ Left_array [] = $ demo [$ I];
} Else {
$ Right_array [] = $ demo [$ I];
}
}
$ Left_array = quick_sort ($ left_array );
$ Right_array = quick_sort ($ right_array );
Return array_merge ($ left_array, array ($ key), $ right_array );
}
$ Test = array ('43 ', '123', '3', '78', '13', '123', '123', '2 ', '56 ', '123', '123', '57', '123 ');
$ Sss = quick_sort ($ test );
Var_dump ($ sss );
?>
Functions ($ demo) {$ num = count ($ demo); for ($ I = 0; $ I $ num; $ I ++) {for ($ j = $ num-1; $ j $ I; $ j --) {if ($ demo [$ j] $ demo [$ j-1]) {...