# php 单例模式排序算法类

2017-01-12 09:58:05来源:oschina作者:醉卧沙场君莫笑人点击

<?php
/**
* 排序类
*/
class Sort
{
//实例对象
private static \$instance = null;
//排序方法
private static \$func = null;
//排序类别
private static \$order = null;
//防止克隆
public function __clone()
{
trigger_error("不能克隆", E_USER_ERROR);
}
//防止实例化
private function __construct(){}
/**
* 获取类实例
* @param string \$func 排序方法
* @param string \$order 排序类别
* @return null|Sort
*/
public static function getInstance()
{
if (!(self::\$instance instanceof self)) {
self::\$instance = new self;
}
return self::\$instance;
}
//进行排序
public function actionSort(\$data = array(), \$order = "asc", \$func = "Bubble")
{
//检查数据是否为数组和是否为空
if (!is_array(\$data) or empty(\$data)) return \$data;
//检查排序类别
self::\$order = strtolower(\$order);
if (!in_array(self::\$order, array("asc", "desc"))) self::\$order = "asc";
//检查方法名称
self::\$func = ucwords(strtolower(\$func));
if (!method_exists(self::\$instance, self::\$func)) self::\$func = "Bubble";
//调用相应排序方法
return call_user_func(array(self::\$instance, self::\$func), \$data);
}
/**
* 冒泡排序法
* @paramarray \$data 排序数组
* @return array
*/
public function Bubble(\$data = array())
{
\$number = count(\$data);
for (\$i = 1; \$i < \$number; \$i++) {
for (\$k = 0; \$k < \$number - \$i; \$k++) {
if (self::\$order == "asc" ? \$data[\$k] > \$data[\$k + 1] : \$data[\$k] < \$data[\$k + 1]) {
\$tmp = \$data[\$k + 1];
\$data[\$k + 1] = \$data[\$k];
\$data[\$k] = \$tmp;
}
}
}
return \$data;
}
/**
* 选择排序法
* @paramarray \$data 排序数组
* @return array
*/
public function Select(\$data = array())
{
\$number = count(\$data);
for (\$i = 0; \$i < \$number - 1; \$i++) {
\$threshold = \$i;
for (\$k = \$i + 1; \$k < \$number; \$k++) {
if (self::\$order == "asc" ? \$data[\$threshold] > \$data[\$k] : \$data[\$threshold] < \$data[\$k]) {
\$threshold = \$k;
}
}
if (\$threshold != \$i) {
\$tmp = \$data[\$i];
\$data[\$i] = \$data[\$threshold];
\$data[\$threshold] = \$tmp;
}
}
return \$data;
}
/**
* 插入排序法
* @param array \$data 排序数组
* @return array
*/
public function Insert(\$data = array())
{
\$number = count(\$data);
for (\$i = 1; \$i < \$number; \$i++) {
\$threshold = \$data[\$i];
for (\$k = \$i - 1; \$k >= 0; \$k--) {
if (self::\$order == "asc" ? \$threshold < \$data[\$k] : \$threshold > \$data[\$k]) {
\$data[\$k + 1] = \$data[\$k];
\$data[\$k] = \$threshold;
} else {
break;
}
}
}
return \$data;
}
/**
* 快速排序法
* @paramarray \$data 排序数组
* @return array
*/
public function Quick(\$data = array())
{
\$number = count(\$data);
if (\$number <= 1) {
return \$data;
}
\$left_data = \$right_data = array();
\$threshold = \$data[0];
for (\$i = 1; \$i < \$number; \$i++) {
if (self::\$order == "asc" ? \$threshold > \$data[\$i] : \$threshold < \$data[\$i]) {
\$left_data[] = \$data[\$i];
} else {
\$right_data[] = \$data[\$i];
}
}
\$left_data = \$this->Quick(\$left_data);
\$right_data = \$this->Quick(\$right_data);
return array_merge(\$left_data, array(\$threshold), \$right_data);
}
}
echo implode(",", Sort::getInstance()->actionSort(array(12, 3, 5, 6, 78, 9), "desc", "Insert"));

无相关信息