<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>waimv.com &#187; 排序</title>
	<atom:link href="http://www.waimv.com/tag/%e6%8e%92%e5%ba%8f/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.waimv.com</link>
	<description></description>
	<lastBuildDate>Fri, 09 Nov 2018 10:41:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>php排序算法</title>
		<link>http://www.waimv.com/php/50/</link>
		<comments>http://www.waimv.com/php/50/#comments</comments>
		<pubDate>Sun, 19 Sep 2010 02:15:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[排序]]></category>

		<guid isPermaLink="false">http://www.szpian.com/?p=50</guid>
		<description><![CDATA[排序]]></description>
			<content:encoded><![CDATA[<p>每年总是要隔三差五的看数据结构，每次总是觉得自己很多东西没有学好，唉。今天贴刚使用php实现4的排序算法，另外堆排序和归并排序没有写。</p>
<p>其他数据结构知识使用php的实现参考我以前写的文章：</p>
<p>http://blog.csdn.net/heiyeshuwu/archive/2006/06/10/787426.aspx</p>
<p>插入排序、选择排序、，冒泡排序，时间复杂度貌似都是 O(N2)，所以实际意义不大，在实际测试中，我对3000个数组元素进行，这三种排序算法都需要花费80秒左右，而快速排序只需要8秒，差距确是比较大，有兴趣的可以自己测试一下。</p>
<pre class="brush:php">

//插入排序（一维数组）
function insert_sort($arr){
    $count = count($arr);
    for($i=1; $i<$count; $i++){
        $tmp = $arr[$i];
        $j = $i - 1;
        while($arr[$j] > $tmp){
            $arr[$j+1] = $arr[$j];
            $arr[$j] = $tmp;
            $j--;
        }
    }
    return $arr;
}

//选择排序（一维数组）
function select_sort($arr){
    $count = count($arr);
    for($i=0; $i<$count; $i++){
        $k = $i;
        for($j=$i+1; $j<$count; $j++){
            if ($arr[$k] > $arr[$j])
                $k = $j;
            if ($k != $i){
                $tmp = $arr[$i];
                $arr[$i] = $arr[$k];
                $arr[$k] = $tmp;
            }
        }
    }
    return $arr;
}

//冒泡排序（一维数组）
function bubble_sort($array){
    $count = count($array);
    if ($count <= 0) return false; 

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

//快速排序（一维数组）
function quick_sort($array){
    if (count($array) <= 1)
     return $array;

    $key = $array[0];
    $left_arr = array();
    $right_arr = array();
    $count=count($array);
    for ( $i=1; $i<$count; $i++ ){

        if ($array[$i] <= $key)
            $left_arr[] = $array[$i];
        else
            $right_arr[] = $array[$i];
   }

    $left_arr = quick_sort($left_arr);
    $right_arr = quick_sort($right_arr); 

    return array_merge($left_arr, array($key), $right_arr);
}
//结束
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.waimv.com/php/50/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
