PHP implements sorting algorithm (1) Bubble sort Quick sort

WBOY
Release: 2016-08-08 09:20:12
Original
1028 people have browsed it

I haven’t practiced my algorithm skills for a long time, so I have almost forgotten many basic algorithms.
There were not many algorithms written in C in the past. So get started today! From now on, I will insist on writing some simple algorithms every night and keep practicing!

The first bubble sort

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

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

以下是用php实现的冒泡排序,
注意以下几点:
1)当带排序的数组为无的时候直接退出,避免出错。
2) 尼玛数组都忘记怎么写了,我真是该死。
</code>
Copy after login
<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> < <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>] < <span>$array</span>[<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>
Copy after login

The second quick sort (quicksort)

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

all the data in the other part are smaller than each other, and then use this method to quickly sort the two parts of the data respectively. The entire sorting process can Proceed recursively, so that the data becomes an ordered sequence.

<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> <= <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> < <span>$count</span>; <span>$i</span>++)</span>{
            if<span>(<span>$arr</span>[<span>$i</span>] <= <span>$key</span>)</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>;</span></span></span></code>
Copy after login

?>

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the sorting algorithm implemented in PHP (1) Bubble sort and quick sort, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!