zoukankan      html  css  js  c++  java
  • 数据结构&算法(PHP描述) 半折插入排序 straight binary sort

    简介:这是数据结构&算法(PHP描述) 半折插入排序 straight binary sort的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

    class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=341542' scrolling='no'>
     1 <?php
    2 /**
    3 * 半折插入排序 straight binary sort
    4 *
    5 * 原理:当直接插入排序进行到某一趟时,对于 r[i] 来讲,前边 i-1 个记录已经按关键字有序。此时不用直接插入排序的方法,而改为折半查找,找出 r[i] 应插的位置,然后插入。
    6 */
    7 function sort_binary_insertion($list)
    8 {
    9 $len = count($list);
    10 if(empty($len)) return $list;
    11
    12 for($i = 1; $i < $len; $i++)
    13 {
    14 $temp = $list[$i];
    15 $low = 0;
    16 $high = $i - 1;
    17
    18 while($low <= $high)
    19 {
    20 $mid = intval(($low + $high)/2);
    21
    22 //if($temp > $list[$mid]) // 从大到小
    23 if($temp < $list[$mid]) // 从小到大
    24 {
    25 $high = $mid - 1;
    26 } else {
    27 $low = $mid + 1;
    28 }
    29 }
    30 for($j = $i - 1; $j >= $mid; $j--)
    31 {
    32 $list[$j + 1] = $list[$j];
    33 echo implode(",",$list),"#mid=",$mid,"<br/>";
    34 }
    35 $list[$low] = $temp;
    36 echo implode(",",$list),"<br/>";
    37 echo "--------------------------------<br/>";
    38 }
    39
    40 return $list;
    41 }
    42
    43
    44 $list = array(4,3,2,1,5,7,3,7);
    45 $list = sort_binary_insertion($list);

      

    爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

    http://biancheng.dnbcw.info/php/341542.html pageNo:6
  • 相关阅读:
    c#中==和equals的比较
    原型指向改变如何添加方法和访问
    把随机数对象暴露给window成为全局变量
    内置对象Array的原型对象中添加方法
    构造函数可以实例化对象
    原型
    无刷新评论
    大量字符串拼接案例
    元素隐藏占位与不占位
    导航栏切换效果案例
  • 原文地址:https://www.cnblogs.com/ooooo/p/2243922.html
Copyright © 2011-2022 走看看