php数据结构之简单选择排序

  1. <?php
  2.     $array = array(13,15,6,10,20,6,3,19);
  3.     $count = count($array);
  4.     $n = $e = 0;
  5.     for($i = 1; $i < $count; $i++)
  6.     {
  7.         //find the min item from $i to last
  8.         $min_index = $i;
  9.         for($j = $i+1; $j < $count; $j++)
  10.         {
  11.             $n++;
  12.             if($array[$j] < $array[$min_index]) $min_index = $j;
  13.         }
  14.       
  15.         if($array[$min_index] < $array[$i-1])
  16.         {
  17.             $e++;
  18.         $tmp = $array[$i-1];
  19.         $array[$i-1] = $array[$min_index];
  20.         $array[$min_index] = $tmp;
  21.         }
  22.     }
  23.     print_r($array);
  24.     echo "共循环{$n}次,共执行{$e}次数据交换";
  25. ?>




执行结果为:

  1. Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
  2. 共循环21次,共执行5次数据交换

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值