Ec (2); php tutorial three common sorting algorithms 1. Bubble Sorting (stable sorting) My understanding: Bubble Sorting is two cycles, large loops and small loops, the size of two consecutive elements starting from the beginning or end. If the elements do not conform to their sorting criteria (from small to large, or from large to small), their values are exchanged. & Lt ;? Php $ arrarray (10, 15, 16, 18, 20, 3, 9, 7, 18, 99); for ($ i1; $ I & lt; 9; $ I ++) & nbsp; & nbsp script ec (2); script
Php tutorial three common sorting algorithms
1. Bubble Sorting (stable sorting)
My personal understanding: Bubble Sorting is the size of two consecutive elements starting from the beginning or end of a large loop with a small loop. If it does not conform to its own sorting standard (from small to large, or from large to small), the value is exchanged.
$ Arr = array (10, 15, 16, 18, 20, 3, 9, 7, 18, 99 );
For ($ I = 1; $ I <= 9; $ I ++)
For ($ j = 1; $ j <= 10-$ I; $ j ++)
If ($ arr [$ J-1]> $ arr [$ j])
{$ Temp = $ arr [$ J-1]; $ arr [$ J-1] = $ arr [$ j]; $ arr [$ j] = $ temp ;}
Echo "after sorted :";
For ($ I = 0; $ I <= 9; $ I ++)
Echo $ arr [$ I]. ",";
?>
Method 2
Function bubble_sort ($ array ){
$ Count = count ($ array );
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j --){
If ($ array [$ j] <$ array [$ J-1]) {// swap value if the value following is smaller than the previous Element
$ Temp = $ array [$ j];
$ Array [$ j] = $ array [$ J-1];
$ Array [$ J-1] = $ temp;
}
}
}
}
2. Fast sorting (also called array sorting) (unstable sorting)
Personal Understanding: Find a key data (standard value) in the array to be sorted, usually take the first element of the array as the key data, and then loop through the array, compare the element value with the key data from the second element. If the value is smaller than the key data, the element is placed on the left of the key value. If the key data is promised, put the element on the right of the key data, save the elements on the left of the key value as an array, and save the elements on the right as an array, and then sort the elements separately, the obtained array and key data are sorted successfully after the array is merged.
Function quick_sort ($ array ){
$ Count = count ($ array );
If ($ count <= 1) retrun $ array; // if the array has only one element or is empty, the array is directly returned without sorting.
$ Key = $ array [0]; // set the first element of the array as key data
$ Left_arr = array ();
$ Right_ar = array ();
For ($ I = 1; $ I <$ count; $ I ++ ){
If ($ array [$ I] <= $ key)
$ Left_arr [] = $ array [$ I];
Else
$ Right_arr [] = $ array [$ I];
}
$ Left_arr = quick_sort ($ left_arr );
$ Right_arr = quick_sort ($ right_arr );
// Return the merged array
Return array_merge ($ left_arr, array ($ key), $ right_arr );
}
3. Select sorting (unstable sorting)
Personal Understanding: Selecting sorting is to select the minimum value and the first element exchange value in the array to be sorted, and then select the minimum value and the second element exchange value from the remaining elements, this loops until the last and last elements are compared.
Function select_sort ($ array ){
$ Count = count ($ array );
If ($ count <= 1) return $ array;
For ($ I = 0; $ I <$ count-1; $ I ++ ){
$ Min = $ array [$ I]; // assume that the current element is the smallest, and adjust it after comparison.
For ($ j = $ I + 1; $ j <$ count; $ j ++ ){
If ($ array [$ j] <$ min ){
$ Min = $ array [$ j];
$ Key = $ j; // enter the key name of the element with the minimum value,
}
}
If ($ min! = $ Array [$ I]) {// If min changes in the loop, you need to exchange data.
$ Temp = $ array [$ I];
$ Array [$ I] = $ array [$ key];
$ Array [$ key] = $ temp;
}
}
}