
正文
php实现数据排序算法 php 排序函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP实现插入排序算法
插入排序(Insertion Sort) 是一种较稳定 简单直观的排序算法 插入排序的工作原理 是通过构建有序序列 对于未排序的数据 在有序序列中从后向前扫描 找到合适的位置并将其插入 插入排序 在最好情况下 时间复杂度为O(n);在最坏情况下 时间复杂度为O(n );平均时间复杂度为O(n )
插入排序示例图
/**
* 数据结构与算法(PHP实现) - 插入排序(Insertion Sort)。Tw.WiNGwit
*
* @author 创想编程(TOPPHP.ORG)
* @copyright Copyright (c) 2013 创想编程(TOPPHP.ORG) All Rights Reserved
* @license /licenses/mit-license.php MIT LICENSE
* @version 1.0.0 - Build20130613
*/
class InsertionSort {
/**
* 需要排序的数据数组。
*
* @var array
*/
private $data;
/**
* 数据数组的长度。
*
* @var integer
*/
private $size;
/**
* 数据数组是否已排序。
*
* @var boolean
*/
private $done;
/**
* 构造方法 - 初始化数据。
*
* @param array $data 需要排序的数据数组。
*/
public function __construct(array $data) {
$this-data = $data;
$this-size = count($this-data);
$this-done = FALSE;
}
/**
* 插入排序。
*/
private function sort() {
$this-done = TRUE;
for ($i = 1; $i $this-size; ++$i) {
$current = $this-data[$i];
if ($current $this-data[$i - 1]) {
for ($j = $i - 1; $j = 0 $this-data[$j] $current; --$j) {
$this-data[$j + 1] = $this-data[$j];
}
$this-data[$j + 1] = $current;
}
}
}
/**
* 获取排序后的数据数组。
*
* @return array 返回排序后的数据数组。
*/
public function getResult() {
if ($this-done) {
return $this-data;
}
$this-sort();
return $this-data;
}
}
?
示例代码 1
2
3
4
$insertion = new InsertionSort(array(9, 1, 5, 3, 2, 8, 6));
echo '
', print_r($insertion-getResult(), TRUE), '
'; lishixinzhi/Article/program/PHP/201311/20783
相关问答
Q1: php几种排序算法实例详解
四种排序算法的PHP实现:
1) 插入排序(Insertion Sort)的基本思想是:
每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止。
2) 选择排序(Selection Sort)的基本思想是:
每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕。
3) 冒泡排序的基本思想是:
两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止。
4) 快速排序实质上和冒泡排序一样,都是属于交换排序的一种应用。所以基本思想和上面的冒泡排序是一样的。
1. sort.php文件如下:
?php
class Sort {
private $arr = array();
private $sort = 'insert';
private $marker = '_sort';
private $debug = TRUE;
/**
* 构造函数
*
* @param array 例如:
$config = array (
'arr' = array(22,3,41,18) , //需要排序的数组值
'sort' = 'insert', //可能值: insert, select, bubble, quick
'debug' = TRUE //可能值: TRUE, FALSE
)
*/
public function construct($config = array()) {
if ( count($config) 0) {
$this-_init($config);
}
}
/**
* 获取排序结果
*/
public function display() {
return $this-arr;
}
/**
* 初始化
*
* @param array
* @return bool
*/
private function _init($config = array()) {
//参数判断
if ( !is_array($config) OR count($config) == 0) {
if ($this-debug === TRUE) {
$this-_log("sort_init_param_invaild");
}
return FALSE;
}
//初始化成员变量
foreach ($config as $key = $val) {
if ( isset($this-$key)) {
$this-$key = $val;
}
}
//调用相应的成员方法完成排序
$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;
}
/**
* 插入排序
*
* @param array
* @return bool
*/
private function insert_sort($arr) {
//参数判断
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log("sort_array(insert)_invaild");
}
return FALSE;
}
//具体实现
$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;
}
/**
* 选择排序
*
* @param array
* @return bool
*/
private function select_sort($arr) {
//参数判断
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log("sort_array(select)_invaild");
}
return FALSE;
}
//具体实现
$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;
}
/**
* 冒泡排序
*
* @param array
* @return bool
*/
private function bubble_sort($arr) {
//参数判断
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log("sort_array(bubble)_invaild");
}
return FALSE;
}
//具体实现
$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;
}
/**
* 快速排序
* @by
* @param array
* @return bool
*/
private function quick_sort($arr) {
//具体实现
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);
}
/**
* 日志记录
*/
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. sort_demo.php文件如下:
?php
require_once('sort.php');
$config = array (
'arr' = array(23, 22, 41, 18, 20, 12, 200303,2200,1192) ,
//需要排序的数组值
'sort' = 'select',
//可能值: insert, select, bubble, quick
'debug' = TRUE
//可能值: TRUE, FALSE
);
$sort = new Sort($config);
//var_dump($config['arr']);
var_dump($sort-display());
/*End of php*/
Q2: php测试数组怎么排序?
1、在test.php文件内php实现数据排序算法,使用header设置test.php执行的编码为utf8php实现数据排序算法,避免输出中文的时候出现乱码。
2、在test.php文件内,创建一个测试的数组,例如,定义一个分类的数组,其对应的索引值分别为0,4,8。
3、在test.php文件内,使用array_values()方法将上一步的数据重新排序,并且从0开始,把重新排序的数组保存在$result变量中。
4、在test.php文件内,使用foreach方法遍历数组,其中$k为索引值,$v为索引值对应的数组值。
5、在test.php文件内,使用echo方法输出数组中的索引值和对应的数组值即可。
php实现数据排序算法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php 排序函数、php实现数据排序算法的信息别忘了在本站进行查找喔。







