|
/**
*
* @author Quanshuidingdang
*/
Class Sort {
Private $arr = Array ();
Private $sort = ' insert ';
Private $marker = ' _sort ';
Private $debug = TRUE;
/**
* Constructor function
*
* @param array For example:
$config = Array (
' arr ' = = Array (22,3,41,18),//arrays value to sort
' 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 sort results
*/
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;
}
Initializing member variables
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 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 sort
*
* @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 Sort
*
* @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;
}
/**
* Quick Sort
*
* @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);
}
/**
* Log records
*/
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 */
|