PHP Sorting Algorithm instance and Sorting Algorithm instance
This example describes the PHP sorting algorithm class. Share it with you for your reference. The details are as follows:
PHP implementation of the four sorting algorithms:
1) the basic idea of Insertion Sort is:
Each time you insert a record to be sorted, insert it to the appropriate position in the subfile according to its keyword size until all records are inserted.
2) the basic idea of Selection Sort is:
Select the record with the smallest keyword from the record to be sorted, and put it at the end of the sorted subfile until all records are sorted.
3) the basic idea of Bubble Sorting is:
Compare the keywords of the records to be sorted. If the order of the two records is the opposite, the records are exchanged until there is no reverse order.
4) Fast sorting is essentially the same as Bubble sorting. It is an application of exchange sorting. So the basic idea is the same as the above Bubble sorting.
1. The sort. php file is as follows:
Signature <? Php/*** @ 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 ), // The array value to be sorted 'sort => 'insert', // possible value: insert, select, bubble, quick 'debug' => TRUE // possible value: TRUE, FALSE) */public function _ construct ($ config = array () {if (count ($ co Nfig)> 0) {$ this-> _ init ($ config) ;}/ *** get sorting result */public function display () {return $ this-> arr;}/*** initialize ** @ param array * @ return bool */private function _ init ($ config = array ()) {// determine 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 the 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 sort ** @ param array * @ return bool */private function insert_sort ($ arr) {// The parameter determines if (! Is_array ($ arr) OR count ($ arr) = 0) {if ($ this-> debug = TRUE) {$ this-> _ log ("sort_array (insert) _ invaild");} return FALSE;} // the specific implementation of $ 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 sort ** @ param array * @ return bool */private function select_sort ($ Rr) {// determine if (! Is_array ($ arr) OR count ($ arr) = 0) {if ($ this-> debug = TRUE) {$ this-> _ log ("sort_array (select) _ invaild");} return FALSE;} // the specific implementation of $ 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 sort ** @ param array * @ return bool */private function bubble_sort ($ arr) {// The parameter determines if (! Is_array ($ arr) OR count ($ arr) = 0) {if ($ this-> debug = TRUE) {$ this-> _ log ("sort_array (bubble) _ invaild");} return FALSE;} // the specific implementation of $ 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 ;} /*** Quick Sort *** @ param array * @ return bool */private function quick_sort ($ arr) {// specific implementation of 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);}/*** log record */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 */
2. The sort_demo.php file is as follows:
Signature <? Phprequire_once ('sort. php '); $ config = array ('arr' => array (23, 22, 41, 18, 20, 12,200 303,2200, 1192 ), // The array value to be sorted 'sort => 'select', // possible value: insert, select, bubble, quick 'debug' => TRUE // possible value: TRUE, FALSE); $ sort = new Sort ($ config); // var_dump ($ config ['arr']); var_dump ($ sort-> display ()); /* End of php */
I hope this article will help you with php programming.