使用php写的算法

<?php
class Sort {
function bubbleSort($a) {
$i=$j=0;
for($i=0;$i<count($a);++$i){
for($j=count($a)-1;$j>$i;--$j){
if($a[$j]<$a[$j-1]){
$this->swap($a,$j,$j-1);
}
}
}
$this->printA($a);
}
function betterBubbleSort($a) {
$i=$j=0;
$flag = true;
for($i=0;$i<count($a)&&$flag;++$i){
$flag = false;
for($j=count($a)-1;$j>$i;--$j){
if($a[$j]<$a[$j-1]){
$this->swap($a,$j,$j-1);
$flag = true;
}
}
}
$this->printA($a);
}
function selectSort($a){
$i=$j=0;
for($i=0;$i<count($a);$i++){
for ($j = $i + 1; $j < count($a); $j++) {
if($a[$i]>$a[$j]){
$this->swap($a,$i,$j);
}
}
}
$this->printA($a);
}
function insertSort($a) {
$i=$j=$temp=0;
for($i=1;$i<count($a);++$i){
$temp = $a[$i];
for($j=$i;$j>0&&$temp<$a[$j-1];--$j){
$a[$j] = $a[$j-1];
}
$a[$j] = $temp;
}
$this->printA($a);
}
function shellSort($a) {
$i=$j=$temp=0;
$increment = count($a);
do{
$increment = floor($increment/3) + 1;/* 设置步长,這是自由设计的,可以设计为increment=increment /5等等 */
for($i=$increment;$i<count($a);++$i){
$temp = $a[$i];/* 进行插入排序 */
for($j=$i;$j>=$increment&&$a[$j-$increment]>$temp;$j-=$increment){
$a[$j]=$a[$j-$increment];
}
$a[$j] = $temp;
}
}while($increment>1);
$this->printA($a);
}
function heapSort($a) {
for ($i = count($a) / 2; $i >= 0; $i--) {/* 把数组构成成大顶堆 */
$this->headAdjust($a, $i, count($a) - 1);
}
for ($i = count($a) - 1; $i > 0; $i--) {/* 把第一个元素和未排序的最后一个交换,然后把新的再构造大定堆 */
$this->swap($a, 0, $i);
$this->headAdjust($a, 0, $i - 1);
}
$this->printA($a);
}
/* 调整堆 */
function headAdjust(&$a, $s, $end) {
$temp = $a[$s];
for ($j = 2 * ($s + 1) - 1; $j <= $end; $j = ($j + 1) * 2 - 1) {
if ($j < $end && $a[$j] < $a[$j + 1])
++$j;
if ($temp >= $a[$j])break;
$a[$s] = $a[$j];
$s = $j;
}
$a[$s] = $temp;
}
function mergeSort($a) {
$temp = array();// 用于暂时存放数据内容
$this->mSort($a,$temp,0,count($a)-1);
$this->printA($a);
}
function mSort(&$a,&$temp,$s,$e){
$mid = floor(($s + $e)/2);
if ($s == $e)return;// 相等表示只有一个,不用排序,直接返回
$this->mSort($a, $temp, $s, $mid);/* 递归调用 */
$this->mSort($a, $temp, $mid + 1, $e);
/* 因为每次调用排序后的数组和原来数组a[]不相同了,而我们要的是后来改变的数组,所以只能在这里初始化temp */
for ($i = $s; $i <= $e; $i++) {
$temp[$i] = $a[$i];
}

$i1 = $s=0;
$i2 = $mid + 1;
for ($cur = $s; $cur <= $e; $cur++) {/* 实现数组归并 */
if ($i1 == $mid + 1) {
$a[$cur] = $temp[$i2++];
} elseif ($i2 == $e + 1) {
$a[$cur] = $temp[$i1++];
} elseif ($temp[$i1] < $temp[$i2]) {
$a[$cur] = $temp[$i1++];
} else {
$a[$cur] = $temp[$i2++];
}
}
}
function quickSort($a) {
$this->qSort($a,0,count($a)-1);
$this->printA($a);
}
function qSort(&$a,$low,$high){ 
$pivot = 0;/* 此值为使得啊a[pivot]大于前面所以值,也大于后面所有值 */
if($low<$high){
$pivot = $this->partition($a,$low,$high);
$this->qSort($a,$low,$pivot-1);
$this->qSort($a,$pivot+1,$high);
}
}


function partition(&$a,$low,$high){
$pivotKey = $a[rand($low,$high)%($high-$low)+$low];// 选择最低处的数为区抽数
while($low<$high){
while($low<$high&&$pivotKey<=$a[$high])$high--;
$this->swap($a,$low,$high);
while($low<$high&&$pivotKey>=$a[$low])$low++;
$this->swap($a,$low,$high);
}
return $low;
}
function swap(&$a,$i,$j){
$temp = $a[$i];
$a[$i] = $a[$j];
$a[$j] = $temp;
}
function printA($a){
for($i=0;$i<count($a);++$i){
echo($a[$i]." &nbsp;&nbsp;");
}
}
}
function getTime() {
$time = microtime();
$times = explode(" ",$time);
return $times[1].$times[0];
}

function main() {
$sort = new Sort();
$start = getTime();
//$sort->bubbleSort($a);
//$sort->betterBubbleSort($a);
//$sort->selectSort($a);
//$sort->insertSort($a);
//$sort->shellSort($a);
//$sort->heapSort($a);
//$sort->mergeSort($a);
$sort->quickSort($a);
$end = getTime();
$diff = $end-$start;
echo('<br>时间:'.$diff.'  &nbsp;&nbsp;');
$sort->printA($a);
}
function O($val,$a) {
$start = getTime();
$sort = new Sort();
@call_user_method ($val, $sort,$a);
//call_user_func (array('Sort',$val,$a));
//call_user_func_array(array('Sort',$val), array($a));
$end = getTime();
$diff = $end-$start;
echo(' 时间:'.$diff.'  &nbsp;&nbsp;<br>');
//$sort->printA($a);
}
$a = [112,55,42,7,14,5,74,6445,5,113,1556,3445,66,100,15,23,1000,2888,4888,23132,2325];
$sortArr = ['bubbleSort','betterBubbleSort','selectSort','insertSort','shellSort','heapSort','mergeSort','quickSort'];
foreach($sortArr as $key => $val) {
O($val,$a);
}

//main();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值