PHP sorting algorithm is fully implemented. When learning PHP, you may encounter a PHP sorting problem. here we will introduce the solution to the PHP sorting problem, and share it with you here. Every year, you may encounter a PHP sorting problem when reading data to learn PHP. here we will introduce the solution to the PHP sorting problem, and share it with you. Every year, we always have to look at the data structure every other time. every time I feel that I have not learned many things well, alas.
Today, I used php to implement the 4 sorting algorithm. In addition, the heap sorting and merge sorting are not written. Insert sorting, select sorting, and bubble sorting. The time complexity seems to be O (N2), so it is of little practical significance. in actual testing, I performed 3000 array elements, it takes about 80 seconds for these three sorting algorithms, and 8 seconds for quick sorting. the gap is indeed large. if you are interested, you can test it on your own. Let's take a detailed look at the implementation of your PHP sorting algorithm.
-
- // Insert sorting (one-dimensional array)
- Function insert_sort ($ arr ){
- $CountCount= Count ($ arr );
- For ($I=1; $ I<$ Count; $ I ++ ){
- $Tmp= $ Arr [$ I];
- $J= $ I-1;
- While ($ arr [$ j]>$ Tmp ){
- $ Arr [$ j + 1] = $ arr [$ j];
- $ Arr [$ j] = $ tmp;
- $ J --;
- }
- }
- Return $ arr;
- }
-
-
- // Select sort (one-dimensional array)
- Function select_sort ($ arr ){
- $CountCount= Count ($ arr );
- For ($I=0; $ I<$ Count; $ I ++ ){
- $K= $ I;
- For ($J= $ I + 1; $ j<$ Count; $ j ++ ){
- If ($ arr [$ k]>$ Arr [$ j])
- $K= $ J;
- If ($ k! = $ I ){
- $Tmp= $ Arr [$ I];
- $ Arr [$ I] = $ arr [$ k];
- $ Arr [$ k] = $ tmp;
- }
- }
- }
- Return $ arr;
- }
-
- // Bubble sort (one-dimensional array)
- Function bubble_sort ($ array ){
- $CountCount= Count ($ array );
- If ($ count<= 0) return false;
-
- 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;
- }
-
- // Fast sorting (one-dimensional array)
- Function quick_sort ($ array ){
- If (count ($ array)<= 1) return $ array;
-
- $Key= $ Array [0];
- $Left_arr=Array();
- $Right_arr=Array();
- For ($I=1; $ I<Count($ Array); $ 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 array_merge ($ left_arr, array ($ key), $ right_arr );
- }
-
- ?>
For the sort PHP sorting problem, here we will introduce the solution to the PHP sorting problem. here we will share it with you. Every year, we always have to look at the data every three to five minutes...