<?php

header("content-type:text/html;charset=utf-8");

$testArr = array();
$time1 = microtime(true);
$testArr = range(1, 10);
$dataLen = count($testArr);
shuffle($testArr);
echo '生成并打乱数组花费时间为:'.(microtime(true) - $time1).'<br>';
//1.冒泡排序
$time1 = microtime(true);
mpsort($testArr);
echo '冒泡排序运行'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'<br>';
//2.简单排序
$time1 = microtime(true);
simpleSort($testArr);
echo '简单排序运行'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'<br>';
//3.插入排序
$time1 = microtime(true);
insertSort($testArr);
echo '插入排序运行'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'<br>';
//4.希尔排序
$time1 = microtime(true);
$memory1 = memory_get_usage(true);
sellSort($testArr);
$maxMemory = memory_get_peak_usage(true);
echo '希尔排序运行'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'分配存储空间为:'.(memory_get_usage(true) - $memory1).'峰值为:'.$maxMemory.'<br>';
//5.快速排序
$time1 = microtime(true);
$memory1 = memory_get_usage(true);
testQuickSort($testArr);
$maxMemory = memory_get_peak_usage(true);
echo '快速排序运行'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'分配存储空间为:'.(memory_get_usage(true) - $memory1).'峰值为:'.$maxMemory.'<br>';
//6.快速排序(调用函数)
$time1 = microtime(true);
$memory1 = memory_get_usage(true);
testQuickSort2($testArr);
$maxMemory = memory_get_peak_usage(true);
echo '快速排序(调用函数)'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'分配存储空间为:'.(memory_get_usage(true) - $memory1).'峰值为:'.$maxMemory.'<br>';
//内置函数
$time1 = microtime(true);
sort($testArr);
$maxMemory = memory_get_peak_usage(true);
echo '内置函数运行'.$dataLen.'次花费时间为:'.(microtime(true) - $time1).'分配存储空间为:'.(memory_get_usage(true) - $memory1).'峰值为:'.$maxMemory.'<br>';
//冒泡排序
function mpsort($testArr){ echo '冒泡排序:<br>';
echo '排序前:';
iteratorArr($testArr);
$dataLen = count($testArr);
for($i=0;$i<$dataLen-1;$i++){
for($j=0;$j<$dataLen-1-$i;$j++){ if($testArr[$j] > $testArr[$j+1]){
$maxVal = $testArr[$j];
$testArr[$j] = $testArr[$j+1];
$testArr[$j+1]= $maxVal;
} }
} echo '排序后:';
iteratorArr($testArr);
} //选择排序(简单排序)
function simpleSort($testArr){
echo '简单排序:<br>';
echo '排序前:';
iteratorArr($testArr);
$dataLen = count($testArr);
$index = 0;
$minVal = 0;
for($i=0;$i<$dataLen;$i++){
$index = $i;
for($j=$i;$j<$dataLen;$j++){
if($testArr[$index] > $testArr[$j]) $index = $j;
} $minVal = $testArr[$index];
$testArr[$index] = $testArr[$i];
$testArr[$i] = $minVal;
} echo '排序后:';
iteratorArr($testArr);
} //插入排序
function insertSort($testArr){
echo '插入排序:<br>';
echo '排序前:';
iteratorArr($testArr); $dataLen = count($testArr);
$index = 0;
$tmp = 0;
//print_r($testArr);
for($i=1;$i<$dataLen;$i++){ if($testArr[$i-1] < $testArr[$i]) continue;
$index = $i;
while($index >=1 && $testArr[$index] < $testArr[$index-1]){
soapArr($testArr , $index , $index-1);
$index--;
}
} echo '排序后:';
iteratorArr($testArr);
} //希尔排序
function sellSort($testArr){
echo '希尔排序:<br>';
echo '排序前:';
iteratorArr($testArr); $dataLen = count($testArr);
$index = 0;
$tmp = 0;
$increment = intval($dataLen/2); for($k =$increment; $k>=1 ;$k--){ for($i=$k;$i<$dataLen;$i += $k){ if($testArr[$i-$k] < $testArr[$i]) continue;
$index = $i;
while($index >=1 && $testArr[$index] < $testArr[$index-$k]){
//soapArr($testArr , $index , $index-$k);
$tmp = $testArr[$index];
$testArr[$index] = $testArr[$index-$k];
$testArr[$index-$k] = $tmp;
$index-=$k;
} } } echo '排序后:';
iteratorArr($testArr);
} function testQuickSort($testArr){
echo '快速排序:<br>';
echo '排序前:';
iteratorArr($testArr); quickSort($testArr , 0 , count($testArr)); echo '排序后:';
iteratorArr($testArr);
} //快速排序
function quickSort(&$testArr , $start , $end){ $index = $start+1;
if($index > $end) return $testArr;
$tmp = 0;
$compareVal = $testArr[$start];
for($i=$index;$i<$end;$i++){ if($testArr[$i] < $compareVal){
//soapArr($testArr , $index , $i);
$tmp = $testArr[$index];
$testArr[$index] = $testArr[$i];
$testArr[$i] = $tmp;
$index++;
}
} //soapArr($testArr , $start , $index-1);
$tmp = $testArr[$start];
$testArr[$start] = $testArr[$index-1];
$testArr[$index-1] = $tmp;
quickSort($testArr , $start , $index-1);
quickSort($testArr, $index, $end);
} //快速排序
function quickSort2(&$testArr , $start , $end){ $index = $start+1;
if($index > $end) return $testArr;
$tmp = 0;
$compareVal = $testArr[$start];
for($i=$index;$i<$end;$i++){ if($testArr[$i] < $compareVal){
soapArr($testArr , $index , $i);
$index++;
}
} soapArr($testArr , $start , $index-1); quickSort2($testArr , $start , $index-1);
quickSort2($testArr, $index, $end);
} function testQuickSort2($testArr){
echo '快速排序(调用函数):<br>';
echo '排序前:';
iteratorArr($testArr); quickSort($testArr , 0 , count($testArr)); echo '快速排序(调用函数)';
iteratorArr($testArr);
} //交换数组
function soapArr(&$arr , $a , $b){
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
} //遍历器
function iteratorArr($arr){ foreach($arr as $v){
echo $v."\n\r";
}
echo "<br>";
}

php实现基础排序算法的更多相关文章

  1. Java面试宝典系列之基础排序算法

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  2. php四种基础排序算法的运行时间比较

    /** * php四种基础排序算法的运行时间比较 * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14 */ //冒泡排序法 ...

  3. Java基础系列--基础排序算法

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9082138.html 一.概述 基础排序算法包括:桶排序.冒泡排序.选择排序.插入排序等 ...

  4. 6种基础排序算法java源码+图文解析[面试宝典]

    一.概述 作为一个合格的程序员,算法是必备技能,特此总结6大基础算法.java版强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步:1.思想2.图 ...

  5. php四种基础排序算法的运行时间比较!

    /** * php四种基础排序算法的运行时间比较 * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14 */ //冒泡排序法 ...

  6. 十大基础排序算法[java源码+动静双图解析+性能分析]

    一.概述 作为一个合格的程序员,算法是必备技能,特此总结十大基础排序算法.java版源码实现,强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步: ...

  7. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

  8. 基础排序算法之并归排序(Merge Sort)

    并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...

  9. ZH奶酪:【数据结构与算法】基础排序算法总结与Python实现

    1.冒泡排序(BubbleSort) 介绍:重复的遍历数列,一次比较两个元素,如果他们顺序错误就进行交换. 2016年1月22日总结: 冒泡排序就是比较相邻的两个元素,保证每次遍历最后的元素最大. 排 ...

  10. 612.1.004 ALGS4 | Elementary Sorts - 基础排序算法

    sublime编辑器写代码,命令行编译 减少对ide的依赖//可以提示缺少什么依赖import 所有示例代码动手敲一遍 Graham's Scan是经典的计算几何算法 shffule 与 map-re ...

随机推荐

  1. 汇编子程序模块化(near&far)

    1: Near 近端使用  C语言实现:  #include <stdio.h>#include <stdlib.h> void print(){ printf("p ...

  2. 利用Python进行数据分析-Pandas(第七部分-时间序列)

    时间序列(time series)数据是一种重要的结构化数据形式,应用于多个领域,包括金融学.经济学.生态学.神经科学.物理学等.时间序列数据的意义取决于具体的应用场景,主要有以下几种: 时间戳(ti ...

  3. vue事件监听机制

    vue事件是同步的.如果绑定了事件(组件标签上绑定事件) 组件的事件触发 组件调用时绑定事件 之后监听事件: $emit 抛出后活等着 $on ,如果监听到了则阻塞执行: 如果为监听到或者未绑定,则会 ...

  4. .net core 中间件使用

    using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; usi ...

  5. 关于javascript中变量及函数的提升

    javascript中变量以及函数的提升,在我们平时的项目中其实还是挺常用的,尤其是大型项目中,不知不觉就会顺手添加一些变量,而有时候自己的不小心就会酿成一些不必要错误,趁有时间整理一下自己对于js中 ...

  6. leaflet 结合 d3.js 实现 geojson 数据地形剖面分析(附源码下载)

    前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...

  7. SAP记账期间变式

        记帐期间变式能够控制每个公司代码中打开的记账期间,包括正常记账期间和特别记账期间.可以为企业组织架构中的每个公司代码定义一个归其单独使用的记账期间变式.      记账期间变式独立于会计年度变 ...

  8. Python—实现钉钉后台开发

    二.实现钉钉免登流程 免登流程分四步:1.前端获取钉钉免登授权码code:2.后端获取access_token:3.使用授权码code和access_token换取用户userid:4.通过acces ...

  9. 表单生成器(Form Builder)之表单数据存储结构mongodb篇

    从这篇笔记开始,记录一下表单生成器(Form Builder)相关的一些东西,网上关于他的介绍有很多,这里就不解释了. 开篇说一下如何存储Form Builder生成的数据.

  10. Linux达人计划(一)

    这时慕课网的Linux基础教学 http://www.imooc.com/view/175 好记性不如烂笔头.一般来说看视频只看不做是很难学好学扎实. 趁着每一节都有采纳的一些笔记.现在对它进行一些归 ...