|
1. the bubble algorithm and sorting algorithm are called Bubble Sorting because they always place decimals forward and large numbers backward in the sorting process.
$ Array = array (a, f, c, B, e, h, j, I, g );
Function maopao_fun ($ array ){
If ($ len <= 1 ){
Return $ arr;
}
$ Count = count ($ array );
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j --){
If ($ array [$ j]> $ array [$ J-1]) {
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ J-1];
$ Array [$ J-1] = $ tmp;
}
}
}
Return $ array;
}
2. Quick sorting,
Quicksort is an improvement in Bubble sorting.
Proposed by C. A. R. Hoare in 1962. The basic idea is: sort the data to be sorted into two separate parts,
All the data in one part is smaller than all the data in the other part, and then sort the data in this way,
The entire sorting process can be performed recursively to convert the entire data into an ordered sequence.
Function quickSort ($ arr ){
$ Len = count ($ arr );
If ($ len <= 1 ){
Return $ arr;
}
$ Key = $ arr [0];
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I <$ len; $ I ++ ){
If ($ arr [$ I] <= $ key ){
$ Left_arr [] = $ arr [$ I];
} Else {
$ Right_arr [] = $ arr [$ I];
}
}
$ Left_arr = quickSort ($ left_arr );
$ Right_arr = quickSort ($ right_arr );
Return array_merge ($ left_arr, array ($ key), $ right_arr );
} |