首页 >后端开发 >php教程 >php实现排序算法(一) 冒泡排序 快速排序

php实现排序算法(一) 冒泡排序 快速排序

WBOY
WBOY原创
2016-08-08 09:20:121086浏览

好久没有来练习的自己的算法技能了以至于很多基本的算法都快忘记了。
以前用c写的算法也不多。那么今天开始吧!以后坚持每天晚上编写一些简单的算法,坚持练习!

第一篇 冒泡排序

<code>冒泡排序是最简单基础的排序,但是由于好久没写代码了,一开始写下去还是不流畅。。。罪过罪过

冒泡排序原理,每次从待排序的序列里面选出一个最大或者最小的元素放到已经排好序的序列后面。知道最后待排序的序列为无;

以下是用php实现的冒泡排序,
注意以下几点:
1)当带排序的数组为无的时候直接退出,避免出错。
2) 尼玛数组都忘记怎么写了,我真是该死。
</code>
<code><span><?php </span><span><span>function</span><span>bubblesort</span><span>(<span>$array</span>)</span>{</span><span>$count</span> = count(<span>$array</span>);

        <span>if</span>(<span>$count</span> == <span>0</span>){
            <span>return</span><span>$array</span>;
        }
        <span>echo</span><span>$count</span>;
        <span>for</span>(<span>$i</span> = <span>0</span>; <span>$i</span> $count</span>; <span>$i</span>++){
            <span>for</span>(<span>$j</span> = <span>$count</span> - <span>1</span>; <span>$j</span> > <span>$i</span>; <span>$j</span>--){
                <span>if</span>(<span>$array</span>[<span>$j</span>] $array[<span>$j</span> - <span>1</span>]){
                    <span>$temp</span> = <span>$array</span>[<span>$j</span>];
                    <span>$array</span>[<span>$j</span>] = <span>$array</span>[<span>$j</span>-<span>1</span>];
                    <span>$array</span>[<span>$j</span>-<span>1</span>] = <span>$temp</span>; 
                }
            }
        }
        <span>return</span><span>$array</span>;

    } 
    <span>$array</span> = <span>array</span>(<span>1</span>,<span>45</span>,<span>2</span>,<span>4</span>,<span>54</span>,<span>2</span>,<span>45</span>,<span>6</span>);
    print_r(bubblesort(<span>$array</span>));
    <span>?></span></code>

第二篇 快速排序(quicksort)

<code>    原理: 快速排序是对冒泡排序的一种改进,基本思想是通过一趟排序将要排序的数据分成两部分,其中的一部
</code>

分比另一部分所有数据都小,然后按此方法对着两部分的数据分别进行快速排序,整个排序过程可以递归进行,以此

达到数据变成有序序列。

<code><span><span><span><?php function quickSort<span>(<span>$arr</span>)</span>{
        <span>$count</span> = count<span>(<span>$arr</span>)</span>;
        if<span>(<span>$count</span> 1</span>)</span>{
            return <span>$arr</span>;
        }
        <span>$key</span> = <span>$arr</span>[<span>0</span>];
        <span>$left_arr</span> = array<span>()</span>;
        <span>$right_arr</span> = array<span>()</span>;
        for<span>(<span>$i</span> = <span>1</span>; <span>$i</span> $count</span>; <span>$i</span>++)</span>{
            if<span>(<span>$arr</span>[<span>$i</span>] $key</span>){
                <span>$left_arr</span>[] = <span>$arr</span>[<span>$i</span>];
            }else{
                <span>$right_arr</span>[] = <span>$arr</span>[<span>$i</span>];
            }
        }

        <span>$left_arr</span> = quickSort<span>(<span>$left_arr</span>)</span>;
        <span>$right_arr</span> = quickSort<span>(<span>$right_arr</span>)</span>;
        return array_merge<span>(<span>$left_arr</span>,array<span>(<span>$key</span>)</span>,<span>$right_arr</span>)</span>;

    }

    <span>$arr</span> = array<span>(<span>23</span>,<span>4</span>,<span>6</span>,<span>46</span>,<span>34</span>,<span>23</span>,<span>6234</span>,<span>34</span>,<span>12</span>,<span>34</span>)</span>;
    print_r<span>(quickSort<span>(<span>$arr</span>)</span>)</span>;</code>

?>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php实现排序算法(一) 冒泡排序 快速排序,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn