php实现基础排序算法
<?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实现基础排序算法的更多相关文章
- Java面试宝典系列之基础排序算法
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- php四种基础排序算法的运行时间比较
/** * php四种基础排序算法的运行时间比较 * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14 */ //冒泡排序法 ...
- Java基础系列--基础排序算法
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9082138.html 一.概述 基础排序算法包括:桶排序.冒泡排序.选择排序.插入排序等 ...
- 6种基础排序算法java源码+图文解析[面试宝典]
一.概述 作为一个合格的程序员,算法是必备技能,特此总结6大基础算法.java版强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步:1.思想2.图 ...
- php四种基础排序算法的运行时间比较!
/** * php四种基础排序算法的运行时间比较 * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14 */ //冒泡排序法 ...
- 十大基础排序算法[java源码+动静双图解析+性能分析]
一.概述 作为一个合格的程序员,算法是必备技能,特此总结十大基础排序算法.java版源码实现,强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步: ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- 基础排序算法之并归排序(Merge Sort)
并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...
- ZH奶酪:【数据结构与算法】基础排序算法总结与Python实现
1.冒泡排序(BubbleSort) 介绍:重复的遍历数列,一次比较两个元素,如果他们顺序错误就进行交换. 2016年1月22日总结: 冒泡排序就是比较相邻的两个元素,保证每次遍历最后的元素最大. 排 ...
- 612.1.004 ALGS4 | Elementary Sorts - 基础排序算法
sublime编辑器写代码,命令行编译 减少对ide的依赖//可以提示缺少什么依赖import 所有示例代码动手敲一遍 Graham's Scan是经典的计算几何算法 shffule 与 map-re ...
随机推荐
- eclipse_neon 的Spket 目录下只有一个Task Tags,没有其他的选项,导致没有办法添加提示文件! 添加sdk文件之后还是没有办法显示的解决办法
问题解决办法: 将 spket-1.6.23的安装包里面的features plugins 单独复制到D:\eclipse_neon\dropins 目录下,重启一下eclipse即可正常显示! 添 ...
- Deepnude算法“tuo”衣服
PS:我不是偷窥狂.我是技术的爱好者 换脸视频后AI又出偏门应用:用算法“tuo”女性衣服 据美国科技媒体Motherboard报道,一名程序员最近开发出一款名叫DeepNude的应用,只要给Deep ...
- Python 从入门到进阶之路(七)
之前的文章我们简单介绍了一下 Python 中异常处理,本篇文章我们来看一下 Python 中 is 和 == 的区别及深拷贝和浅拷贝. 我们先来看一下在 Python 中的双等号 == . == 是 ...
- c#中的Nullable(可空类型)
在C#中使用Nullable类型(给整型赋null值的方法) 在C#1.x的版本中,一个值类型变量是不可以被赋予null值的,否则会产生异常.在C#2.0中,微软提供了Nullable类型,允许用它定 ...
- 前vue.js+elementui,后koa2,nodejs搭建网站
1,安装 nodejs,npm 2,使用 npm 安装 vue,vue-cli 3,使用脚手架搭建项目,添加依赖:axios,vue-router,elementui,vuex 等 4,建立 rout ...
- [转]Outlook 2016 will not display Web linked images
本文转自:https://community.spiceworks.com/topic/1952626-outlook-2016-will-not-display-web-linked-images ...
- windows 应急流程及实战演练
前言 本文摘自信安之路公众号的文章. 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事 ...
- 更改组织属性-以更改maxrecordsforexporttoexcel为例
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复232或者20161101可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
- Kafka学习(一)
官网 kafka.apache.org 集群部署 消息中间键 --> 分布式流式平台 Kafka Streaming Flume: 1个进程包含三个角色 source channle sink ...
- sqlite3 国产化如何添加密码
sqlite3 国产化如何添加密码 sqlite3 国产化如何添加密码sqlite3 国产化如何添加密码