Bytes /** * * @ Author quanshuidingdang */ Class Sort { Private $ arr = array (); Private $ sort = 'insert '; Private $ marker = '_ sort '; Private $ debug = TRUE; /** * Constructor * * @ Param array example: $ Config = array ( 'Arr' => array (22,3, 41,18), // array value to be sorted 'Sort '=> 'insert', // possible values: insert, select, bubble, and quick 'Debug' => TRUE // possible values: TRUE, FALSE ) */ Public function _ construct ($ config = array ()){ If (count ($ config)> 0 ){ $ This-> _ init ($ config ); } } /** * Obtain the sorting result */ Public function display (){ Return $ this-> arr; } /** * Initialization * * @ Param array * @ Return bool */ Private function _ init ($ config = array ()){ // Parameter determination If (! Is_array ($ config) OR count ($ config) = 0 ){ If ($ this-> debug === TRUE ){ $ This-> _ log ("sort_init_param_invaild "); } Return FALSE; } // Initialize the member variable Foreach ($ config as $ key => $ val ){ If (isset ($ this-> $ key )){ $ This-> $ key = $ val; } } // Call the corresponding member method to complete sorting $ Method = $ this-> sort. $ this-> marker; If (! Method_exists ($ this, $ method )){ If ($ this-> debug === TRUE ){ $ This-> _ log ("sort_method_invaild "); } Return FALSE; } If (FALSE ===( $ this-> arr = $ this-> $ method ($ this-> arr ))) Return FALSE; Return TRUE; } /** * Insert sorting * * @ Param array * @ Return bool */ Private function insert_sort ($ arr ){ // Parameter determination If (! Is_array ($ arr) OR count ($ arr) = 0 ){ If ($ this-> debug === TRUE ){ $ This-> _ log ("sort_array (insert) _ invaild "); } Return FALSE; } // Specific implementation $ Count = count ($ arr ); For ($ I = 1; $ I <$ count; $ I ++ ){ $ Tmp = $ arr [$ I]; For ($ j = $ I-1; $ j> = 0; $ j --){ If ($ arr [$ j]> $ tmp ){ $ Arr [$ j + 1] = $ arr [$ j]; $ Arr [$ j] = $ tmp; } } } Return $ arr; } /** * Select sorting * * @ Param array * @ Return bool */ Private function select_sort ($ arr ){ // Parameter determination If (! Is_array ($ arr) OR count ($ arr) = 0 ){ If ($ this-> debug === TRUE ){ $ This-> _ log ("sort_array (select) _ invaild "); } Return FALSE; } // Specific implementation $ Count = count ($ arr ); For ($ I = 0; $ I <$ count-1; $ I ++ ){ $ Min = $ I; For ($ j = $ I + 1; $ j <$ count; $ j ++ ){ If ($ arr [$ min]> $ arr [$ j]) $ min = $ j; } If ($ min! = $ I ){ $ Tmp = $ arr [$ min]; $ Arr [$ min] = $ arr [$ I]; $ Arr [$ I] = $ tmp; } } Return $ arr; } /** * Bubble sorting * * @ Param array * @ Return bool */ Private function bubble_sort ($ arr ){ // Parameter determination If (! Is_array ($ arr) OR count ($ arr) = 0 ){ If ($ this-> debug === TRUE ){ $ This-> _ log ("sort_array (bubble) _ invaild "); } Return FALSE; } // Specific implementation $ Count = count ($ arr ); For ($ I = 0; $ I <$ count; $ I ++ ){ For ($ j = $ count-1; $ j> $ I; $ j --){ If ($ arr [$ j] <$ arr [$ j-1]) { $ Tmp = $ arr [$ j]; $ Arr [$ j] = $ arr [$ j-1]; $ Arr [$ j-1] = $ tmp; } } } Return $ arr; } /** * Fast sorting * * @ Param array * @ Return bool */ Private function quick_sort ($ arr ){ // Specific implementation If (count ($ arr) <= 1) return $ arr; $ Key = $ arr [0]; $ Left_arr = array (); $ Right_arr = array (); For ($ I = 1; $ I <count ($ arr); $ I ++ ){ If ($ arr [$ I] <= $ key) $ Left_arr [] = $ arr [$ I]; Else $ Right_arr [] = $ arr [$ I]; } $ Left_arr = $ this-> quick_sort ($ left_arr ); $ Right_arr = $ this-> quick_sort ($ right_arr ); Return array_merge ($ left_arr, array ($ key), $ right_arr ); } /** * Logging */ Private function _ log ($ msg ){ $ Msg = 'date ['. date ('Y-m-d H: I: s').'] '. $ msg.' \ n '; Return @ file_put_contents ('sort _ err. log', $ msg, FILE_APPEND ); } } /* End of file sort. php */ /* Location htdocs/sort. php */ |