排序--归并排序

<?php
class Test
{
  public function shellSort(&$arr)
  {
    $this->sort($arr,0,count($arr)-1);
  }
  public function sort(&$arr,$lo,$hi)
  {
     if($lo >= $hi)
     {
       return;
     }
     $tempNum =intval(($hi - $lo)/2);
     $mid = $lo + $tempNum;
     $this->sort($arr,$lo,$mid);
     $this->sort($arr,$mid+1,$hi);
     $this->merge($arr,$lo,$mid,$hi);
     print_r($arr);
  }

  public function merge(&$arr,$lo,$mid,$hi)
  {

    $m = $lo;
    $n = $mid+1;

    for($k = $lo;$k <= $hi;$k++)
    {
      $temps[$k] = $arr[$k];
    }

    for($i = $lo; $i<=$hi; $i++)
    {
      if($m > $mid)
      {
        $arr[$i] = $temps[$n++];
      }
      else if($n > $hi)
      {
        $arr[$i] = $temps[$m++];
      }
      else if($temps[$m] > $temps[$n])
      {
        $arr[$i] = $temps[$n++];
      }
      else
      {
        $arr[$i] = $temps[$m++];
      }
    }
  }
}

$test = new Test();
$params = array(5,8,7,6);
$test->shellSort($params);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值