﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-MyMSDN-随笔分类-algorithm</title><link>http://www.cppblog.com/mymsdn/category/9728.html</link><description>MyMSDN记录开发新知道</description><language>zh-cn</language><lastBuildDate>Fri, 07 Aug 2009 10:06:01 GMT</lastBuildDate><pubDate>Fri, 07 Aug 2009 10:06:01 GMT</pubDate><ttl>60</ttl><item><title>static in C</title><link>http://www.cppblog.com/mymsdn/archive/2009/05/24/85626.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Sun, 24 May 2009 10:17:00 GMT</pubDate><guid>http://www.cppblog.com/mymsdn/archive/2009/05/24/85626.html</guid><wfw:comment>http://www.cppblog.com/mymsdn/comments/85626.html</wfw:comment><comments>http://www.cppblog.com/mymsdn/archive/2009/05/24/85626.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/mymsdn/comments/commentRss/85626.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mymsdn/services/trackbacks/85626.html</trackback:ping><description><![CDATA[<!--MyMSDN style!--><link rel="stylesheet" type="text/css" href="http://files.cnblogs.com/volnet/gocool.css"> <style type="text/css">
body{background-color:#eeeeee;}
</style>  <p></p><pre class="gc-code"><span style="color: #7f0055">#include </span><span style="color: #2a00ff">&lt;stdio.h&gt;
</span><span style="color: #7f0055">#include </span><span style="color: #2a00ff">&lt;stdlib.h&gt;

</span><span style="color: #7f0055">char </span>* favorite_fruit1(<span style="color: #7f0055">void</span>);
<span style="color: #7f0055">char </span>* favorite_fruit2(<span style="color: #7f0055">void</span>);
<span style="color: #7f0055">void </span>favorite_fruit3(<span style="color: #7f0055">char </span>**);
<span style="color: #7f0055">int </span>main(<span style="color: #7f0055">void</span>) {
    <span style="color: #7f0055">char </span>* fruit1 = favorite_fruit1();
    <span style="color: #642880">printf</span>(<span style="color: #2a00ff">"%s\n"</span>, fruit1);

    <span style="color: #7f0055">char </span>* fruit2 = favorite_fruit2();
    <span style="color: #642880">printf</span>(<span style="color: #2a00ff">"%s\n"</span>, fruit2);

    <span style="color: #7f0055">char </span>* fruit3 = NULL;
    favorite_fruit3(&amp;fruit3);
    <span style="color: #642880">printf</span>(<span style="color: #2a00ff">"%s\n"</span>, fruit3);

    <span style="color: #642880">printf</span>(<span style="color: #2a00ff">"------END of CODE------"</span>);
    <span style="color: #7f0055">return </span>EXIT_SUCCESS;
}
<span style="color: #7f0055">char </span>* favorite_fruit1(<span style="color: #7f0055">void</span>){
    <span style="color: #7f0055">char </span>deciduous[] = <span style="color: #2a00ff">"apple"</span>;
    <span style="color: #7f0055">return </span>deciduous;
}
<span style="color: #7f0055">char </span>* favorite_fruit2(<span style="color: #7f0055">void</span>){
    <span style="color: #7f0055">static char </span>deciduous[] = <span style="color: #2a00ff">"apple"</span>;
    <span style="color: #7f0055">return </span>deciduous;
}
<span style="color: #7f0055">void </span>favorite_fruit3(<span style="color: #7f0055">char </span>** fruit){
    <span style="color: #7f0055">static char </span>deciduous[] = <span style="color: #2a00ff">"apple"</span>;
    *fruit = deciduous;
}</pre>
<p>favorite_fruit1很明显会出现问题，原因是因为<span style="color: #7f0055">char </span>deciduous[]是局部变量，在函数调用返回后，就释放了。</p>
<p>favorite_fruit2因为使用了static，而static限定了变量被保存在数据段（data segment）中，它的声明周期同程序一样长。所以不会出错。</p>
<p>favorite_fruit3是另一种有效的写法，其原理同2。</p> <img src ="http://www.cppblog.com/mymsdn/aggbug/85626.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mymsdn/" target="_blank">volnet</a> 2009-05-24 18:17 <a href="http://www.cppblog.com/mymsdn/archive/2009/05/24/85626.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QuickSort快速排序法</title><link>http://www.cppblog.com/mymsdn/archive/2009/03/06/quicksort.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Thu, 05 Mar 2009 19:03:00 GMT</pubDate><guid>http://www.cppblog.com/mymsdn/archive/2009/03/06/quicksort.html</guid><wfw:comment>http://www.cppblog.com/mymsdn/comments/75693.html</wfw:comment><comments>http://www.cppblog.com/mymsdn/archive/2009/03/06/quicksort.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/mymsdn/comments/commentRss/75693.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mymsdn/services/trackbacks/75693.html</trackback:ping><description><![CDATA[<p>快速排序法：（好土，感觉满世界都会，不过还是写一下，当然了，标准库里多的是排序算法），这里还是实现经典版的快速排序了，时间复杂度O(nlogn)</p> <p>Algorithms.h</p><pre class="gc-code"><span style="color: blue">#pragma once

#include </span><span style="color: #a31515">&lt;iostream&gt;

</span><span style="color: blue">class </span>Algorithms
{
<span style="color: blue">public</span>:
    Algorithms(<span style="color: blue">void</span>);
    ~Algorithms(<span style="color: blue">void</span>);

<span style="color: blue">public</span>:
    <span style="color: blue">template </span>&lt;<span style="color: blue">typename </span>T&gt;
    <span style="color: blue">static void </span>QuickSort(T* arr, size_t min, size_t max);
<span style="color: blue">private</span>:
    <span style="color: blue">template </span>&lt;<span style="color: blue">typename </span>T&gt;
    <span style="color: blue">static </span>size_t qsort_helper_partition(T* arr, size_t min, size_t max);
    <span style="color: blue">template </span>&lt;<span style="color: blue">typename </span>T&gt;
    <span style="color: blue">static inline void </span>swap(T* arr, size_t x, size_t y);
};

<span style="color: blue">template </span>&lt;<span style="color: blue">typename </span>T&gt;
<span style="color: blue">void </span>Algorithms::QuickSort(T* arr, size_t min, size_t max)
{
    <span style="color: blue">if</span>(min &gt;= max || max == 0 - 1) <span style="color: blue">return</span>;
    size_t p = qsort_helper_partition(arr, min, max);

    QuickSort(arr, min, p - 1);
    QuickSort(arr, p + 1, max);
}

<span style="color: blue">template </span>&lt;<span style="color: blue">typename </span>T&gt;
size_t Algorithms::qsort_helper_partition(T* arr, size_t min, size_t max)
{
    T cmp = arr[min];
    <span style="color: blue">int </span>i = min + 1, j = max;
    <span style="color: blue">while</span>(<span style="color: blue">true</span>)
    {
        <span style="color: blue">while</span>(cmp &lt; arr[i])
            ++i;
        <span style="color: blue">while</span>(arr[j] &lt; cmp)
            --j;
        <span style="color: blue">if</span>(i &gt;= j) <span style="color: blue">break</span>;

        swap(arr, i, j);
    }
    swap(arr, min, j);
    <span style="color: blue">return </span>j;
}

<span style="color: blue">template </span>&lt;<span style="color: blue">typename </span>T&gt;
<span style="color: blue">void </span>Algorithms::swap(T* arr, size_t x, size_t y)
{
    T tmp = arr[x];
    arr[x] = arr[y];
    arr[y] = tmp;
}</pre>
<p>用法：（顺便有标准库的排序法，当然只是调一下，没有什么可说的了）<pre class="gc-code"><span style="color: blue">#include </span><span style="color: #a31515">"Algorithms.h"
</span><span style="color: blue">#include </span><span style="color: #a31515">&lt;iostream&gt;
</span><span style="color: blue">#include </span><span style="color: #a31515">&lt;vector&gt;
</span><span style="color: blue">#include </span><span style="color: #a31515">&lt;algorithm&gt;

</span><span style="color: blue">int </span>_tmain(<span style="color: blue">int </span>argc, _TCHAR* argv[])
{
    <span style="color: blue">int </span>arr[] = {4, 8, 3, 7, 1, 5, 6, 2};

    <span style="color: blue">for</span>(size_t i = 0; i != 8; ++i)
    {
        std::cout&lt;&lt;arr[i]&lt;&lt;<span style="color: #a31515">" "</span>;
    }
    std::cout&lt;&lt;std::endl;

    Algorithms::QuickSort(arr,0, 7);

    <span style="color: blue">for</span>(size_t i = 0; i != 8; ++i)
    {
        std::cout&lt;&lt;arr[i]&lt;&lt;<span style="color: #a31515">" "</span>;
    }
    std::cout&lt;&lt;std::endl;

    std::vector&lt;<span style="color: blue">int</span>&gt; vec;
    vec.push_back(3);
    vec.push_back(1);
    vec.push_back(4);
    vec.push_back(1);
    vec.push_back(7);
    vec.push_back(6);

    <span style="color: blue">for</span>(std::vector&lt;<span style="color: blue">int</span>&gt;::iterator iter = vec.begin();
        iter != vec.end(); ++ iter)
    {
        std::cout&lt;&lt;*iter&lt;&lt;<span style="color: #a31515">" "</span>;
    }
    std::cout&lt;&lt;std::endl;

    std::sort(vec.begin(), vec.end());

    <span style="color: blue">for</span>(std::vector&lt;<span style="color: blue">int</span>&gt;::iterator iter = vec.begin();
        iter != vec.end(); ++ iter)
    {
        std::cout&lt;&lt;*iter&lt;&lt;<span style="color: #a31515">" "</span>;
    }
    std::cout&lt;&lt;std::endl;

    <span style="color: blue">return </span>0;
}

</pre> <img src ="http://www.cppblog.com/mymsdn/aggbug/75693.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mymsdn/" target="_blank">volnet</a> 2009-03-06 03:03 <a href="http://www.cppblog.com/mymsdn/archive/2009/03/06/quicksort.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>