<?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. linux命令-挂载命令

    一.挂载命令 1.mount 命令基本格式 linux 所有存储设备都必须挂载使用,包括硬盘 命令名称:mount 命令所在路径:/bin/mount 执行权限:所有用户 [root@localhos ...

  2. UTXO和Account模型一个都不能少

    UTXO对于非区块链从业人员来说可能比较陌生,UTXO的全称是Unspent Transaction Output,这中本聪在比特币中的一个天才设计.而Account模型就很常见,也很容易理解,你银行 ...

  3. 某小公司RESTful、共用接口、前后端分离、接口约定的实践

    作者:邵磊 juejin.im/post/59eafab36fb9a045076eccc3 前言 随着互联网高速发展,公司对项目开发周期不断缩短,我们面对各种需求,使用原有对接方式,各端已经很难快速应 ...

  4. 记录一次netcore3.0 code first使用迁移命令报错问题

    环境描述:macOS .vscode .netcore3.0 迁移工具:Microsoft.EntityFrameworkCore.Tools 遇到的问题: Could not execute bec ...

  5. vue-cli 引用elementUI打包后文件过大

    解决方案:使用externals引用第三方资源,防止element资源被打包到自己项目中,(总共修改3个页面index.html.webpack.base.conf.js.main.js) 1.修改i ...

  6. 移动端H5页面遇到的问题总结(转载请注明出处)

    最近刚做完一个移动端的项目,产品之无敌,过程之艰辛,我就不多说了,记录下在这个项目中遇到的问题,以防万一,虽然这些可能都是已经被N多前辈解决掉了的问题,也放在这里,算是为自己漫漫前端路铺了一颗小石子儿 ...

  7. Angular中使用bootstrap样式

    Angular中使用bootstrap样式 Angular中引入bootstrap的方法   方法1:在Angular.json中的styles数组中添加bootstrap路径 如下所示: " ...

  8. SAP记账期间变式

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

  9. P1005 Spell It Right

    # P1005 Spell It Right 原题 Given a non-negative integer N, your task is to compute the sum of all the ...

  10. Linux IO协议栈

    图片来源自网络,保持更新:更多内容请关注 cnblogs.com/xuyaowen 参考链接: https://zhuanlan.zhihu.com/p/39721251 http://blog.yu ...