Four sorting algorithms: PHP implementation class and four sorting algorithms: php

Source: Internet
Author: User
Tags array example

Four sorting algorithms: PHP implementation class and four sorting algorithms: php

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.

Refer:Http://www.lai18.com/content/433167.html

 

The following is the implementation code:

<? Php/*** @ author quanshuidingdang * @ edit http://www.lai18.com */class Sort {private $ arr = array (); private $ sort = 'insert '; private $ marker = '_ sort'; private $ debug = TRUE;/*** constructor * @ edit http://www.lai18.com * @ 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 values: TRUE, FALSE) */public Function _ construct ($ config = array () {if (count ($ config)> 0) {$ this-> _ init ($ config );}} /*** get the sorting result */public function display () {return $ this-> arr ;} /*** initialize ** @ param array * @ return bool */private function _ init ($ config = array () {// The parameter determines 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 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.