资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php数据结构算法实例 php 数据处理

php几种排序算法实例详解

四种排序算法的PHP实现:

创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为广丰企业提供专业的网站制作、做网站,广丰网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

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*/

php数组遍历类与用法示例

本文实例讲述了php数组遍历类与用法。分享给大家供大家参考,具体如下:

?php

class

scanArray{

public

$arr;

public

$where;

private

$str;

public

function

scan($arr,$where="array"){

$this-arr

=

$arr;

$this-where

=

$where;

foreach($this-arr

as

$k=$v){

if(is_array($v)){

$this-where

=

($this-where)."[{$k}]";

$this-scan($v,$this-where);

}else{

$this-str

.=

$this-where."[{$k}]=".$v.'br

/';

}

}

return

$this-str;

}

function

__destruct(){

unset($this-arr);

unset($this-where);

}

}

$a

=

array('g'="a",'vv'=array("b"="b","l"="c","xx"=array("e","g")));

$ah

=

new

scanArray();

$b

=

$ah-scan($a);

echo

$b;

运行结果:

array[g]=a

array[vv][b]=b

array[vv][l]=c

array[vv][xx][0]=e

array[vv][xx][1]=g

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》及《PHP常用遍历算法与技巧总结》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:PHP遍历数组的方法汇总PHP

数组遍历方法大全(foreach,list,each)PHP

数组遍历foreach语法结构及实例PHP中多维数组的foreach遍历示例php实现遍历多维数组的方法PHP中使用foreach()遍历二维数组的简单实例PHP遍历数组的三种方法及效率对比分析PHP实现的操作数组类库定义与用法示例PHP数组操作类实例PHP数组生成XML格式数据的封装类实例

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


本文题目:php数据结构算法实例 php 数据处理
分享地址:http://cdkjz.cn/article/doeichj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220