PHP implementations of the four sorting algorithms:
1) The basic idea of inserting a sort (insertion sort) is:
Insert a record to be sorted each time, by its keyword size, into the appropriate location in the previously sorted sub-file until all records are inserted.
2) The basic idea of selecting sort (Selection sort) is :
Each trip selects the smallest record of the keyword from the record to be sorted, placing the order at the end of the ordered sub-file until all records have been sorted.
3) The basic idea of bubble sorting is :
22 Compare the keywords for the records to be sorted, and find that the two records are exchanged in reverse order until there are no reversed records.
4) Fast sorting is essentially the same as bubbling sort, which is an application that belongs to the Exchange sort. So the basic idea is the same as the bubble sort above.
Reference:http://www.lai18.com/content/433167.html
below 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 for example: $config = Array (' arr ' = = Array (22,3,41,18),/ /need to sort array values ' sort ' = ' insert ',//Possible values: Insert, select, Bubble, quick ' debug ' + true//possible value: TRUE, FALSE) */ Public function __construct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config); }}/** * Get sorting results */Public Function display () {return $this->arr; }/** * Initialize * * @param array * @return BOOL */Private Function _init ($config = Array ()) {//parameter to determine if ( !is_array ($config) OR count ($config) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_init_param_i Nvaild "); } return FALSE; }//Initialize member variable foreach ($config as $key = = $val) {if (Isset ($this-> $key)) {$this, $key = $val; }}//Call the corresponding Member method to complete the sort $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 to determine if (! is_ Array ($arr) OR count ($arr) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_array (insert) _invaild" ); } return FALSE; }//Concrete 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 sort * * @param array * @return BOOL */private Function Select_sort ($arr) {//parameter to determine if (! Is_array ($arr) OR count ($arr) = = 0) {if ($this->debug = = = TRUE) { $this->_log ("Sort_array (SELECT) _invaild"); } return FALSE; }//Concrete 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 sort * * @param array * @return BOOL */Private Function Bubble_sort ($arr) {//parameter to determine if (! is_ Array ($arr) OR count ($arr) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_array (bubble) _invaild" ); } return FALSE; }//Concrete 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; }/** * 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 */
Four Sorting algorithms PHP implementation class