首页 > php教程 > php手册 > 正文

php冒泡排序快速排序

WBOY
发布: 2016-07-06 13:30:46
原创
921 人浏览过

/****** 1)冒泡排序:两两交换数值,最小的值在最左边,就如最轻的气泡在最上边。 2)对整列数两两交换一次,最小的数在最左边,每次都能得一个在剩下的数中的最小 的数,冒出来的数组成一个有序区间,剩下的值组成一无序区间,且有序区间中每一元素值都比无

/******
1)冒泡排序:两两交换数值,最小的值在最左边,就如最轻的气泡在最上边。
2)对整列数两两交换一次,最小的数在最左边,每次都能得一个在剩下的数中的最小 的数,“冒”出来的数组成一个有序区间,剩下的值组成一无序区间,且有序区间中每一元素值都比无序区间的小。
3)快速排序:基准数,左右二个数组,递归调用,合并。
4)插入排序:排序区间分成二部分,左边有序,右边无序,从右区间取第一个元素插入左区间,若此元素比左边区间最右边的元素大,留在原处,若此元素比左 边区间最右边的元素小,则插在最右边5)元素的原位置,同时最右边元素右移一位,计算器减一,重新和前面的元素比较,直到前面的元素比要插入元素小为止,重复 上述步骤。
6)注意区间端点值的处理,及数组的第一个元素下标为0.
***/

<span style="color: #800080;"><br>$a</span>=<span style="color: #0000ff;">array</span>('3','8','1','4','11','7'<span style="color: #000000;">);
</span><span style="color: #008080;">PRint_r</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);
</span><span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">从小到大</span>
<span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1;<span style="color: #800080;">$i</span>$len;<span style="color: #800080;">$i</span>++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>-1;<span style="color: #800080;">$j</span>>=<span style="color: #800080;">$i</span>;<span style="color: #800080;">$j</span>--<span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>]$a[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">])
{</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了</span>
 <span style="color: #800080;">$x</span>=<span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
 </span><span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
 </span><span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>-1]=<span style="color: #800080;">$x</span><span style="color: #000000;">;
}
}
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">另一种方法 从小到大</span>
<span style="color: #800080;">$b</span>=<span style="color: #0000ff;">array</span>('4','3','8','9','2','1'<span style="color: #000000;">);
</span><span style="color: #800080;">$len</span>=<span style="color: #008080;">count</span>(<span style="color: #800080;">$b</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$k</span>=1;<span style="color: #800080;">$k</span>$len;<span style="color: #800080;">$k</span>++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>-1,<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$len-<span style="color: #800080;">$k</span>;<span style="color: #800080;">$i</span>++,<span style="color: #800080;">$j</span>--<span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>]$b[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">]){
</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了</span>
 <span style="color: #800080;">$tmp</span>=<span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
 </span><span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
 </span><span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>-1]=<span style="color: #800080;">$tmp</span><span style="color: #000000;">;
}
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$b</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "
"<span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">下面的这个执行效率更高</span>
<span style="color: #0000ff;">function</span> maopao(<span style="color: #800080;">$arr</span><span style="color: #000000;">)
{
 </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
 </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1; <span style="color: #800080;">$i</span>$len; <span style="color: #800080;">$i</span>++)<span style="color: #008000;">//</span><span style="color: #008000;">最多做n-1趟排序</span>
<span style="color: #000000;"> {
  </span><span style="color: #800080;">$flag</span> = <span style="color: #0000ff;">false</span>;    <span style="color: #008000;">//</span><span style="color: #008000;">本趟排序开始前,交换标志应为假</span>
  <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>-1;<span style="color: #800080;">$j</span>>=<span style="color: #800080;">$i</span>;<span style="color: #800080;">$j</span>--<span style="color: #000000;">)
  {
   </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>]$arr[<span style="color: #800080;">$j</span>-1])<span style="color: #008000;">//</span><span style="color: #008000;">交换记录</span>
   {<span style="color: #008000;">//</span><span style="color: #008000;">如果是从大到小的话,只要在这里的判断改成if($arr[$j]>$arr[$j-1])就可以了</span>
     <span style="color: #800080;">$x</span>=<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
     </span><span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
     </span><span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>-1]=<span style="color: #800080;">$x</span><span style="color: #000000;">;
     </span><span style="color: #800080;">$flag</span> = <span style="color: #0000ff;">true</span>;<span style="color: #008000;">//</span><span style="color: #008000;">发生了交换,故将交换标志置为真</span>
<span style="color: #000000;">   }
  }
  </span><span style="color: #0000ff;">if</span>(! <span style="color: #800080;">$flag</span>)<span style="color: #008000;">//</span><span style="color: #008000;">本趟排序未发生交换,提前终止算法</span>
  <span style="color: #0000ff;">return</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;   
 }
}
</span><span style="color: #800080;">$shuz</span> = <span style="color: #0000ff;">array</span>('2','4','1','8','5'<span style="color: #000000;">);
</span><span style="color: #800080;">$bb</span> = maopao(<span style="color: #800080;">$shuz</span><span style="color: #000000;">);
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$bb</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 快速排序</span>
<span style="color: #0000ff;">function</span> kuaisu(<span style="color: #800080;">$arr</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$len</span> ){
        <span style="color: #0000ff;">return</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
    }
    </span><span style="color: #800080;">$key</span> = <span style="color: #800080;">$arr</span>[0<span style="color: #000000;">];
    </span><span style="color: #800080;">$left_arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$right_arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1; <span style="color: #800080;">$i</span>$len;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span>] $key<span style="color: #000000;">){
            </span><span style="color: #800080;">$left_arr</span>[] = <span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$right_arr</span>[] = <span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
        }
    }
    </span><span style="color: #800080;">$left_arr</span> = kuaisu(<span style="color: #800080;">$left_arr</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$right_arr</span> = kuaisu(<span style="color: #800080;">$right_arr</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$left_arr</span>, <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$key</span>), <span style="color: #800080;">$right_arr</span><span style="color: #000000;">);
}
</span><span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span>(23,98,54,2,9,62,34<span style="color: #000000;">);
</span><span style="color: #008080;">print_r</span>(kuaisu(<span style="color: #800080;">$arr</span>));
登录后复制

 


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板