PHP Sort Algorithm class instance

Source: Internet
Author: User

This example describes the PHP sort algorithm class. Share to everyone for your reference. Specifically as follows:

PHP implementation of the four sort algorithms:

1 The basic idea of the insertion sort (insertion sort) is:

Each time a record to be sorted is inserted at its key size to the appropriate location in the previously sorted child file until all records are inserted.

2 The basic idea of choosing sort (Selection sort) is:

Each trip selects the smallest key record from the record to be sorted, in the order that it is placed at the end of the sorted sub file until all the records have finished sorting.

3 The basic idea of bubble sort is:

22 Compare the keywords of the records to be sorted, and find that the order of the two records is reversed to exchange until there is no reverse sequence of records.

4 The quick sort is essentially the same as the bubble sort, which is an application of commutative ordering. So the basic idea is the same as the bubble sort above.

1. The sort.php document reads as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 <?php/** * * @author Quanshuidingdang/class Sort {Private $arr = array (); private $sort = ' insert '; private $mar Ker = ' _sort '; Private $debug = TRUE; /** * constructor * @param array for example: $config = Array (' arr ' => Array (22,3,41,18),//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 ($config) > 0) {$this->_init ($config);}} /** * Get sort result/Public function display () {return $this->arr;}/** * Initialize * @param array * @return BOOL/private F Unction _init ($config = Array ()) {//Parameter judgment if (!is_array ($config) OR count ($config) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_init_param_invaild");} return FALSE; }//Initialize member variable foreach ($config as $key => $val) {if isset ($this-> $key)) {$this-> $key = $val;}//Call the appropriate member method to complete the row Order $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) {//Parameter judgment if (! Is_array ($arr) OR count ($arr) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_array (insert) _invaild");} return FALSE;}//Specific implementation $cou NT = 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 ($arr) {//Parameter judgment if (! Is_array ($arr) OR count ($arr) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_array (SELECT) _invaild");} return FALSE;}//Specific implementation $cou NT = count ($arr); for ($i = 0; $i < $count-1; $i + +) {$min = $i; for ($j = $i +1; $j < $count; $j + +) {if ($arr [$min] > $arr [$j]) $m in = $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) {//Parameter judgment if (! Is_array ($arr) OR count ($arr) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_array (Bubble) _invaild");} return FALSE;}//Specific implementation $cou NT = 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) {//Implement 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 document reads as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 <?php require_once (' sort.php '); $config = Array (' arr ' => Array (23, 22, 41, 18, 20, 12, 200303,2200,1192),//array value to be sorted ' sort ' => ' select ',//possible value: I Nsert, 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 your PHP program design.

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.