锘??xml version="1.0" encoding="utf-8" standalone="yes"?>狠狠久久亚洲欧美专区,欧美片在线观看,欧美婷婷六月丁香综合色http://m.shnenglu.com/zerolee/category/15205.htmlzh-cnSat, 16 Jun 2012 15:17:32 GMTSat, 16 Jun 2012 15:17:32 GMT60濡備綍灝嗕竴鐗囧唴瀛橀摼鎺ユ垚閾捐〃http://m.shnenglu.com/zerolee/archive/2012/06/16/179073.htmlZero LeeZero LeeSat, 16 Jun 2012 10:38:00 GMThttp://m.shnenglu.com/zerolee/archive/2012/06/16/179073.htmlhttp://m.shnenglu.com/zerolee/comments/179073.htmlhttp://m.shnenglu.com/zerolee/archive/2012/06/16/179073.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/179073.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/179073.html
榪欓噷緇欏嚭SGI STL鍐呭瓨鍒嗛厤鍣ㄧ殑涓涓畝鍗曞疄鐜幫細
棣栧厛瀹氫箟涓涓猽nion鏁版嵁緇撴瀯錛?br />
1 union obj {
2     union obj* free_list_link;
3     char client_data[1];
4 };

榪欎釜union緇撴瀯浣撶殑鏈澶уぇ灝忎負4bytes 錛堝湪32bits 騫沖彴涓婏級錛?bytes 錛堝湪64bits騫沖彴涓婏級銆?br />
鍋囪閭g墖鍐呭瓨鐨勫湴鍧涓篶hunk錛岄偅涔堟垜浠彲浠ヨ繖鏍峰仛錛?  

 1 obj* current_obj, *next_obj;
 2
 next_obj = (obj*)chunk;
 3 for (int i = 0; ; i++) {
 4     current_obj = next_obj;
 5     next_obj = (obj*)((char*)next_obj + m);
 6     if (n - 1 == i) {
 7         current_obj -> free_list_link = 0;
 8         break;
 9     } else {
10         current_obj -> free_list_link = next_obj;
11     }
12 }
13 


Zero Lee 2012-06-16 18:38 鍙戣〃璇勮
]]>
姹備竴涓鏁存暟鐨勫鉤鏂規(guī)牴紼嬪簭瀹炵幇http://m.shnenglu.com/zerolee/archive/2011/10/19/158661.htmlZero LeeZero LeeWed, 19 Oct 2011 01:37:00 GMThttp://m.shnenglu.com/zerolee/archive/2011/10/19/158661.htmlhttp://m.shnenglu.com/zerolee/comments/158661.htmlhttp://m.shnenglu.com/zerolee/archive/2011/10/19/158661.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/158661.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/158661.html閲囩敤鍔犳硶閫掑鐨勬柟寮忔潵浠f浛涔樻硶涓嶯榪涜姣旇緝錛岄掑鏄寜鐓х瓑宸暟鍒楃殑鏂瑰紡銆?br />
 1 int square(int n)
 2 {
 3     int tmp = 0;
 4     for (int i = 1; i < n; i++) {
 5         tmp += 2*(i-1)+1;
 6         if (tmp == n)
 7             return i;
 8         continue;
 9     }
10     if (n!=0) {
11         printf("no integer sqare found!\n");
12         tmp = -1;
13     }
14     return tmp;
15 }
16 


Zero Lee 2011-10-19 09:37 鍙戣〃璇勮
]]>
涓緇勬暟鐨勫叏鎺掑垪鍜岀粍鍚堢▼搴忓疄鐜?/title><link>http://m.shnenglu.com/zerolee/archive/2011/10/19/158660.html</link><dc:creator>Zero Lee</dc:creator><author>Zero Lee</author><pubDate>Wed, 19 Oct 2011 01:34:00 GMT</pubDate><guid>http://m.shnenglu.com/zerolee/archive/2011/10/19/158660.html</guid><wfw:comment>http://m.shnenglu.com/zerolee/comments/158660.html</wfw:comment><comments>http://m.shnenglu.com/zerolee/archive/2011/10/19/158660.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/zerolee/comments/commentRss/158660.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/zerolee/services/trackbacks/158660.html</trackback:ping><description><![CDATA[鏄劇ず涓緇勬暟鐨勫叏鎺掑垪鍜岀粍鍚堢▼搴忥細 <div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080; "> 1</span> <span style="color: #0000FF; ">void</span><span style="color: #000000; "> print(</span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> std::vector</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">>&</span><span style="color: #000000; "> s)<br /> </span><span style="color: #008080; "> 2</span> <span style="color: #000000; ">{<br /> </span><span style="color: #008080; "> 3</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> n </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br /> </span><span style="color: #008080; "> 4</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d:</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, n</span><span style="color: #000000; ">++</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; "> 5</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">[</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; "> 6</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> s.size(); i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br /> </span><span style="color: #008080; "> 7</span> <span style="color: #000000; ">        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; "> %d </span><span style="color: #000000; ">"</span><span style="color: #000000; ">, s[i]);<br /> </span><span style="color: #008080; "> 8</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">]\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; "> 9</span> <span style="color: #000000; ">}<br /> </span><span style="color: #008080; ">10</span> <span style="color: #000000; "><br /> </span><span style="color: #008080; ">11</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> permutation(std::vector</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">>&</span><span style="color: #000000; "> v, </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> beg, </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> end)<br /> </span><span style="color: #008080; ">12</span> <span style="color: #000000; ">{<br /> </span><span style="color: #008080; ">13</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (beg </span><span style="color: #000000; ">></span><span style="color: #000000; "> end) {<br /> </span><span style="color: #008080; ">14</span> <span style="color: #000000; ">        print(v);<br /> </span><span style="color: #008080; ">15</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br /> </span><span style="color: #008080; ">16</span> <span style="color: #000000; ">    }<br /> </span><span style="color: #008080; ">17</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> beg; i </span><span style="color: #000000; "><=</span><span style="color: #000000; "> end; i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">) {<br /> </span><span style="color: #008080; ">18</span> <span style="color: #000000; ">        std::swap(v[i], v[beg]);<br /> </span><span style="color: #008080; ">19</span> <span style="color: #000000; ">        permutation(v, beg</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">, end); </span><span style="color: #008000; ">//</span><span style="color: #008000; "> pleate note, here always beg+1, not i+1</span><span style="color: #008000; "><br /> </span><span style="color: #008080; ">20</span> <span style="color: #008000; "></span><span style="color: #000000; ">        std::swap(v[i], v[beg]);<br /> </span><span style="color: #008080; ">21</span> <span style="color: #000000; ">    }<br /> </span><span style="color: #008080; ">22</span> <span style="color: #000000; ">}<br /> </span><span style="color: #008080; ">23</span> <span style="color: #000000; "><br /> </span><span style="color: #008080; ">24</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> pm(std::vector</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">>&</span><span style="color: #000000; "> v)<br /> </span><span style="color: #008080; ">25</span> <span style="color: #000000; ">{<br /> </span><span style="color: #008080; ">26</span> <span style="color: #000000; ">    std::copy(v.begin(), v.end(), std::ostream_iterator</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">></span><span style="color: #000000; ">(std::cout, </span><span style="color: #000000; ">"</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">));<br /> </span><span style="color: #008080; ">27</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">\nfull permulation are:\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; ">28</span> <span style="color: #000000; ">    permutation(v, </span><span style="color: #000000; ">0</span><span style="color: #000000; ">, v.size()</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; ">29</span> <span style="color: #000000; ">}<br /> </span><span style="color: #008080; ">30</span> <span style="color: #000000; "><br /> </span><span style="color: #008080; ">31</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> print(</span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> std::vector</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">>&</span><span style="color: #000000; "> v, </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> beg, </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> end)<br /> </span><span style="color: #008080; ">32</span> <span style="color: #000000; ">{<br /> </span><span style="color: #008080; ">33</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> n </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br /> </span><span style="color: #008080; ">34</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d:</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, n</span><span style="color: #000000; ">++</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; ">35</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">[</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; ">36</span> <span style="color: #000000; ">    std::copy(v.begin()</span><span style="color: #000000; ">+</span><span style="color: #000000; ">beg, v.begin()</span><span style="color: #000000; ">+</span><span style="color: #000000; ">end</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">, std::ostream_iterator</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">></span><span style="color: #000000; ">(std::cout, </span><span style="color: #000000; ">"</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">));<br /> </span><span style="color: #008080; ">37</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">]\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; ">38</span> <span style="color: #000000; ">}<br /> </span><span style="color: #008080; ">39</span> <span style="color: #000000; "><br /> </span><span style="color: #008080; ">40</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> fullcombination(std::vector</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">>&</span><span style="color: #000000; "> v)<br /> </span><span style="color: #008080; ">41</span> <span style="color: #000000; ">{<br /> </span><span style="color: #008080; ">42</span> <span style="color: #000000; ">    printf(</span><span style="color: #000000; ">"</span><div style="display: inline-block; "></div>full combination<span style="color: #000000; "> are:\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /> </span><span style="color: #008080; ">43</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (unsigned </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> v.size(); i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">) {<br /> </span><span style="color: #008080; ">44</span> <span style="color: #000000; ">        print(v, i, i);<br /> </span><span style="color: #008080; ">45</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (unsigned </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> j </span><span style="color: #000000; ">=</span><span style="color: #000000; "> i</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">; j </span><span style="color: #000000; "><</span><span style="color: #000000; "> v.size(); j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">) {<br /> </span><span style="color: #008080; ">46</span> <span style="color: #000000; ">            </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> k </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; k </span><span style="color: #000000; "><</span><span style="color: #000000; "> v.size()</span><span style="color: #000000; ">-</span><span style="color: #000000; ">j; k</span><span style="color: #000000; ">++</span><span style="color: #000000; ">) {<br /> </span><span style="color: #008080; ">47</span> <span style="color: #000000; ">                std::swap(v[j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">k], v[j]);<br /> </span><span style="color: #008080; ">48</span> <span style="color: #000000; ">                print(v, i, j);<br /> </span><span style="color: #008080; ">49</span> <span style="color: #000000; ">                std::swap(v[j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">k], v[j]);<br /> </span><span style="color: #008080; ">50</span> <span style="color: #000000; ">            }<br /> </span><span style="color: #008080; ">51</span> <span style="color: #000000; ">        }<br /> </span><span style="color: #008080; ">52</span> <span style="color: #000000; ">    }<br /> </span><span style="color: #008080; ">53</span> <span style="color: #000000; ">}<br /> </span><span style="color: #008080; ">54</span> <span style="color: #000000; "></span></div><img src ="http://m.shnenglu.com/zerolee/aggbug/158660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/zerolee/" target="_blank">Zero Lee</a> 2011-10-19 09:34 <a href="http://m.shnenglu.com/zerolee/archive/2011/10/19/158660.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Generating Permutationshttp://m.shnenglu.com/zerolee/archive/2011/09/21/156428.htmlZero LeeZero LeeWed, 21 Sep 2011 07:22:00 GMThttp://m.shnenglu.com/zerolee/archive/2011/09/21/156428.htmlhttp://m.shnenglu.com/zerolee/comments/156428.htmlhttp://m.shnenglu.com/zerolee/archive/2011/09/21/156428.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/156428.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/156428.htmlA permutation can be obtained by selecting an element in the given set and recursively permuting the remaining elements.

 { ai,P(a1,...,ai-1,ai+1,...,aN) if N > 1 P(a1,...,aN) = aN if N = 1


 --|--|--|-| |a|b-|c-d-| a|------------b------------c-------------d --|--|--|-| ---|-|--|--| ---|--|-|--| --|--|--|-| |-|b-|c-d-| |a-|-|c-|d-| |a-|b-|-|d-| |a|b-|c-|-|

At each stage of the permutation process, the given set of elements consists of two parts: a subset of values that already have been processed, and a subset that still needs to be processed. This logical seperation can be physically realized by exchanging, in the i’th step, the i’th value with the value being chosen at that stage. That approaches leaves the first subset in the first i locations of the outcome.

 --|--|--|-| |a|b-|c-d-| --|--|------------|--------------------------|--|-| a||b |c d | |b a |c |d | |c||b |a|d | |d|b |c |a| -----|--------------------------|---- ----------- --|--|--|-| ---|-|--|--| ---|--|-|--| b-|a-|c-d-| |b-|c|a-|d-| |b-|d-|c|a-| ---|--|------------|--|-| |b-|c-a-|d-| b-|c-|d-|a| | b-|c-|d-|a| |-|--|--|-|
permute(i) 
   if i == N  output A[N] 
   else 
      for j = i to N do 
         swap(A[i], A[j]) 
         permute(i+1) 
         swap(A[i], A[j]) 


Zero Lee 2011-09-21 15:22 鍙戣〃璇勮
]]>
蹇熸帓搴廋++瀹炵幇http://m.shnenglu.com/zerolee/archive/2011/09/16/155940.htmlZero LeeZero LeeFri, 16 Sep 2011 06:15:00 GMThttp://m.shnenglu.com/zerolee/archive/2011/09/16/155940.htmlhttp://m.shnenglu.com/zerolee/comments/155940.htmlhttp://m.shnenglu.com/zerolee/archive/2011/09/16/155940.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/155940.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/155940.html
  1 #include <vector>
  2 #include <iterator>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <cstdlib>
  6 #include <ctime>
  7 #include <cassert>
  8 
  9 void printv(const std::vector<int>& v)
 10 {
 11     std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
 12     std::cout << std::endl;
 13 }
 14 
 15 void printv(const std::vector<int>& v, int l, int h)
 16 {
 17     std::copy(v.begin()+l, v.begin()+h+1, std::ostream_iterator<int>(std::cout, " "));
 18     std::cout << std::endl;
 19 }
 20 
 21 int exchange(int& a, int& b)
 22 {
 23     int tmp = a;
 24     a = b;
 25     b = tmp;
 26 }
 27 
 28 int partition1(std::vector<int>& v, int l, int h)
 29 {
 30     int i = l-1;
 31     int pivot = v[h];
 32     int j = l;
 33     while (j < h) {
 34         if (v[j] < pivot) {
 35             i++;
 36             exchange(v[i], v[j]);
 37         }
 38         j++;
 39     }
 40     exchange(v[i+1], v[h]);
 41     return i+1;
 42 }
 43 
 44 int partition2(std::vector<int>& v, int l, int h)
 45 {
 46     int m = l+(h-l)/2;
 47     int i = l;
 48     int j = h;
 49 
 50     int pivot = v[m];
 51     while (1) {
 52         while (v[i] < pivot) i++;
 53         while (v[j] > pivot) j--;
 54         if (!(i<j)) {
 57             return j;
 58         }
 59         exchange(v[i], v[j]);
 60         i++;j--;
 61     }
 62 }
 63 
 64 void myQuickSort1(std::vector<int>& v, int l, int h)
 65 {
 66 /*
 67   Please note, returned m is the loc of pivot,
 68   in below myQuickSort1, can remove m, since left of m, all <= pivot
 69   right of m, all >= pivot
 70  */
 71     if (l < h) {
 72         int m = partition1(v, l, h);
 73         myQuickSort1(v, l, m-1);
 74         myQuickSort1(v, m+1, h);
 75     }
 76 }
 77 
 78 void myQuickSort2(std::vector<int>& v, int l, int h)
 79 {
 80 /*
 81  if partition2 return i, please call below
 82   myQuickSort2(v, l, m-1);
 83   myQuickSort2(v, m, h);
 84  else if it return j, please call below
 85   myQuickSort2(v, l, m);
 86   myQuickSort2(v, m+1, h);
 87 */
 88     if (l < h) {
 89         int m = partition2(v, l, h);
 90         myQuickSort2(v, l, m);
 91         myQuickSort2(v, m+1, h);
 92     }
 93 }
 94 
 95 int main()
 96 {
 97 
 98     const int N = 100;
 99     srand(time(0));
100     std::vector<int> v, vtmp;
101     for (int i = 0; i < N; i++)
102         v.push_back(int(rand()%N));
103     vtmp.insert(vtmp.end(), v.begin(), v.end());
104     std::cout << "Original data sequence is:\n";
105     std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
106     std::cout << std::endl;
107     myQuickSort1(v, 0, N-1);
108     std::cout << "After quicksort(1), the data sequence is:\n";
109     std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
110     std::cout << std::endl;
111 
112     myQuickSort2(vtmp, 0, N-1);
113     std::cout << "After quicksort(2), the data sequence is:\n";
114     std::copy(vtmp.begin(), vtmp.end(), std::ostream_iterator<int>(std::cout, " "));
115     std::cout << std::endl;
116 
117     assert(v==vtmp);
118     return 0;
119 }
120 


Zero Lee 2011-09-16 14:15 鍙戣〃璇勮
]]>
鍫嗘帓搴忕畻娉曠殑涓涓疄鐜?/title><link>http://m.shnenglu.com/zerolee/archive/2011/03/25/142686.html</link><dc:creator>Zero Lee</dc:creator><author>Zero Lee</author><pubDate>Fri, 25 Mar 2011 01:32:00 GMT</pubDate><guid>http://m.shnenglu.com/zerolee/archive/2011/03/25/142686.html</guid><wfw:comment>http://m.shnenglu.com/zerolee/comments/142686.html</wfw:comment><comments>http://m.shnenglu.com/zerolee/archive/2011/03/25/142686.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/zerolee/comments/commentRss/142686.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/zerolee/services/trackbacks/142686.html</trackback:ping><description><![CDATA[Pseudcode<span style="font-weight: bold;"><br> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;"> 1</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">function heapSort(a, count) </span><span style="color: #0000ff; font-weight: normal;">is</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;"> 2</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     input:  an unordered array a of length count<br></span><span style="color: #008080; font-weight: normal;"> 3</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"> <br></span><span style="color: #008080; font-weight: normal;"> 4</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     (first place a </span><span style="color: #0000ff; font-weight: normal;">in</span><span style="color: #000000; font-weight: normal;"> max-heap order)<br></span><span style="color: #008080; font-weight: normal;"> 5</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     heapify(a, count)<br></span><span style="color: #008080; font-weight: normal;"> 6</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"> <br></span><span style="color: #008080; font-weight: normal;"> 7</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     end := count-1 </span><span style="color: #008000; font-weight: normal;">//in languages with zero-based arrays the children are 2*i+1 and 2*i+2<br></span><span style="color: #008080; font-weight: normal;"> 8</span><span style="font-weight: normal;"> </span><span style="color: #008000; font-weight: normal;"></span><span style="color: #000000; font-weight: normal;">     </span><span style="color: #0000ff; font-weight: normal;">while</span><span style="color: #000000; font-weight: normal;"> end > 0 </span><span style="color: #0000ff; font-weight: normal;">do</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;"> 9</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (swap the root(maximum value) of the heap with the last element of the heap)<br></span><span style="color: #008080; font-weight: normal;">10</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         swap(a[end], a[0])<br></span><span style="color: #008080; font-weight: normal;">11</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (put the heap back </span><span style="color: #0000ff; font-weight: normal;">in</span><span style="color: #000000; font-weight: normal;"> max-heap order)<br></span><span style="color: #008080; font-weight: normal;">12</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         siftDown(a, 0, end-1)<br></span><span style="color: #008080; font-weight: normal;">13</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (decrease the size of the heap by one so that the previous max value will<br></span><span style="color: #008080; font-weight: normal;">14</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         stay </span><span style="color: #0000ff; font-weight: normal;">in</span><span style="color: #000000; font-weight: normal;"> its proper placement)<br></span><span style="color: #008080; font-weight: normal;">15</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         end := end - 1<br></span><span style="color: #008080; font-weight: normal;">16</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"> <br></span><span style="color: #008080; font-weight: normal;">17</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"> function heapify(a, count) </span><span style="color: #0000ff; font-weight: normal;">is</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;">18</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     (start </span><span style="color: #0000ff; font-weight: normal;">is</span><span style="color: #000000; font-weight: normal;"> assigned the index </span><span style="color: #0000ff; font-weight: normal;">in</span><span style="color: #000000; font-weight: normal;"> a of the last parent node)<br></span><span style="color: #008080; font-weight: normal;">19</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     start := count / 2 - 1<br></span><span style="color: #008080; font-weight: normal;">20</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     <br></span><span style="color: #008080; font-weight: normal;">21</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     </span><span style="color: #0000ff; font-weight: normal;">while</span><span style="color: #000000; font-weight: normal;"> start ≥ 0 </span><span style="color: #0000ff; font-weight: normal;">do</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;">22</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (sift down the node at index start to the proper place such that all nodes below<br></span><span style="color: #008080; font-weight: normal;">23</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">          the start index are </span><span style="color: #0000ff; font-weight: normal;">in</span><span style="color: #000000; font-weight: normal;"> heap order)<br></span><span style="color: #008080; font-weight: normal;">24</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         siftDown(a, start, count-1)<br></span><span style="color: #008080; font-weight: normal;">25</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         start := start - 1<br></span><span style="color: #008080; font-weight: normal;">26</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     (after sifting down the root all nodes/elements are </span><span style="color: #0000ff; font-weight: normal;">in</span><span style="color: #000000; font-weight: normal;"> heap order)<br></span><span style="color: #008080; font-weight: normal;">27</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"> <br></span><span style="color: #008080; font-weight: normal;">28</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"> function siftDown(a, start, end) </span><span style="color: #0000ff; font-weight: normal;">is</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;">29</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     input:  end represents the limit of how far down the heap<br></span><span style="color: #008080; font-weight: normal;">30</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">                   to sift.<br></span><span style="color: #008080; font-weight: normal;">31</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     root := start<br></span><span style="color: #008080; font-weight: normal;">32</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;">33</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">     </span><span style="color: #0000ff; font-weight: normal;">while</span><span style="color: #000000; font-weight: normal;"> root * 2 + 1 ≤ end </span><span style="color: #0000ff; font-weight: normal;">do</span><span style="color: #000000; font-weight: normal;">          (While the root has at least one child)<br></span><span style="color: #008080; font-weight: normal;">34</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         child := root * 2 + 1        (root*2 + 1 points to the left child)<br></span><span style="color: #008080; font-weight: normal;">35</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         swap := root        (keeps track of child to swap with)<br></span><span style="color: #008080; font-weight: normal;">36</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (check </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> root </span><span style="color: #0000ff; font-weight: normal;">is</span><span style="color: #000000; font-weight: normal;"> smaller than left child)<br></span><span style="color: #008080; font-weight: normal;">37</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> a[swap] < a[child]<br></span><span style="color: #008080; font-weight: normal;">38</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">             swap := child<br></span><span style="color: #008080; font-weight: normal;">39</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (check </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> right child exists, and </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> it's bigger than what we're currently swapping with)<br></span><span style="color: #008080; font-weight: normal;">40</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> child < end and a[swap] < a[child+1]<br></span><span style="color: #008080; font-weight: normal;">41</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">             swap := child + 1<br></span><span style="color: #008080; font-weight: normal;">42</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         (check </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> we need to swap at all)<br></span><span style="color: #008080; font-weight: normal;">43</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         </span><span style="color: #0000ff; font-weight: normal;">if</span><span style="color: #000000; font-weight: normal;"> swap != root<br></span><span style="color: #008080; font-weight: normal;">44</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">             swap(a[root], a[swap])<br></span><span style="color: #008080; font-weight: normal;">45</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">             root := swap          (repeat to </span><span style="color: #0000ff; font-weight: normal;">continue</span><span style="color: #000000; font-weight: normal;"> sifting down the child now)<br></span><span style="color: #008080; font-weight: normal;">46</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">         </span><span style="color: #0000ff; font-weight: normal;">else</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;">47</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;">             </span><span style="color: #0000ff; font-weight: normal;">return</span><span style="color: #000000; font-weight: normal;"><br></span><span style="color: #008080; font-weight: normal;">48</span><span style="font-weight: normal;"> </span><span style="color: #000000; font-weight: normal;"></span></div> <br style="font-weight: normal;"><span style="font-weight: normal;">C++ 瀹炵幇</span><br style="font-weight: normal;"> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; font-weight: normal;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;"> build one maximum heap</span><span style="color: #008000;"><br></span><span style="color: #008080;"> 2</span> <span style="color: #008000;"></span><span style="color: #000000;">template </span><span style="color: #000000;"><</span><span style="color: #000000;">typename T</span><span style="color: #000000;">></span><span style="color: #000000;"><br></span><span style="color: #008080;"> 3</span> <span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> heapsort(std::vector</span><span style="color: #000000;"><</span><span style="color: #000000;">T</span><span style="color: #000000;">>&</span><span style="color: #000000;"> a)<br></span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{<br></span><span style="color: #008080;"> 5</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> sz </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)a.size();<br></span><span style="color: #008080;"> 6</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> sz</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">; i </span><span style="color: #000000;">>=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i</span><span style="color: #000000;">--</span><span style="color: #000000;">)<br></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">        percDown(a, i, sz);<br></span><span style="color: #008080;"> 8</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">build heap is done.\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> sz</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">; j </span><span style="color: #000000;">></span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; j</span><span style="color: #000000;">--</span><span style="color: #000000;">) {<br></span><span style="color: #008080;">10</span> <span style="color: #000000;">        std::swap(a[</span><span style="color: #000000;">0</span><span style="color: #000000;">], a[j]);<br></span><span style="color: #008080;">11</span> <span style="color: #000000;">        percDown(a, </span><span style="color: #000000;">0</span><span style="color: #000000;">, j);<br></span><span style="color: #008080;">12</span> <span style="color: #000000;">    }<br></span><span style="color: #008080;">13</span> <span style="color: #000000;">}<br></span><span style="color: #008080;">14</span> <span style="color: #000000;"><br></span><span style="color: #008080;">15</span> <span style="color: #000000;">inline </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> leftChild(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i)<br></span><span style="color: #008080;">16</span> <span style="color: #000000;">{<br></span><span style="color: #008080;">17</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">18</span> <span style="color: #000000;">}<br></span><span style="color: #008080;">19</span> <span style="color: #000000;"><br></span><span style="color: #008080;">20</span> <span style="color: #000000;">template </span><span style="color: #000000;"><</span><span style="color: #000000;">typename T</span><span style="color: #000000;">></span><span style="color: #000000;"><br></span><span style="color: #008080;">21</span> <span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> percDown(std::vector</span><span style="color: #000000;"><</span><span style="color: #000000;">T</span><span style="color: #000000;">>&</span><span style="color: #000000;"> a, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n)<br></span><span style="color: #008080;">22</span> <span style="color: #000000;">{<br></span><span style="color: #008080;">23</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> child;<br></span><span style="color: #008080;">24</span> <span style="color: #000000;">    T tmp;<br></span><span style="color: #008080;">25</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (tmp </span><span style="color: #000000;">=</span><span style="color: #000000;"> a[i]; leftChild(i) </span><span style="color: #000000;"><</span><span style="color: #000000;"> n; i </span><span style="color: #000000;">=</span><span style="color: #000000;"> child) {<br></span><span style="color: #008080;">26</span> <span style="color: #000000;">        child </span><span style="color: #000000;">=</span><span style="color: #000000;"> leftChild(i);<br></span><span style="color: #008080;">27</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (child </span><span style="color: #000000;">!=</span><span style="color: #000000;"> n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;"> </span><span style="color: #000000;">&&</span><span style="color: #000000;"> a[child] </span><span style="color: #000000;"><</span><span style="color: #000000;"> a[child</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">])<br></span><span style="color: #008080;">28</span> <span style="color: #000000;">            child</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">29</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (tmp </span><span style="color: #000000;"><</span><span style="color: #000000;"> a[child])<br></span><span style="color: #008080;">30</span> <span style="color: #000000;">            a[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> a[child];<br></span><span style="color: #008080;">31</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br></span><span style="color: #008080;">32</span> <span style="color: #000000;">            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br></span><span style="color: #008080;">33</span> <span style="color: #000000;">    }<br></span><span style="color: #008080;">34</span> <span style="color: #000000;">    a[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> tmp;<br></span><span style="color: #008080;">35</span> <span style="color: #000000;">}<br></span><span style="color: #008080;">36</span> <span style="color: #000000;"></span></div> </span><span style="font-weight: bold;"><br></span> <img src ="http://m.shnenglu.com/zerolee/aggbug/142686.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/zerolee/" target="_blank">Zero Lee</a> 2011-03-25 09:32 <a href="http://m.shnenglu.com/zerolee/archive/2011/03/25/142686.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍒犻櫎涓庢煇涓瓧絎︾浉閭諱笖鐩稿悓鐨勫瓧絎?/title><link>http://m.shnenglu.com/zerolee/archive/2011/03/22/142504.html</link><dc:creator>Zero Lee</dc:creator><author>Zero Lee</author><pubDate>Tue, 22 Mar 2011 10:40:00 GMT</pubDate><guid>http://m.shnenglu.com/zerolee/archive/2011/03/22/142504.html</guid><wfw:comment>http://m.shnenglu.com/zerolee/comments/142504.html</wfw:comment><comments>http://m.shnenglu.com/zerolee/archive/2011/03/22/142504.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/zerolee/comments/commentRss/142504.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/zerolee/services/trackbacks/142504.html</trackback:ping><description><![CDATA[鍒犻櫎涓庢煇涓瓧絎︾浉閭諱笖鐩稿悓鐨勫瓧絎︼紝濡傦紝abcddef錛屽垹闄ょ浉閭?cè)潥勭浉鍚屽瓧绗鍚庡彉涓猴細 abcdef銆?br>瑕佹眰錛氳緭鍏ュ瓧絎︿覆,杈撳嚭鍒犻櫎鍚庣殑緇撴灉銆?br>鍙傝僑TL綆楁硶錛?unique/unique_copy<br><br> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> deldupchar(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> s)<br></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{<br></span><span style="color: #008080;"> 3</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> i, </span><span style="color: #000000;">*</span><span style="color: #000000;">j;    <br></span><span style="color: #008080;"> 4</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (s && *s!='\0') {<br></span><span style="color: #008080;"> 5</span> <span style="color: #000000;">        i </span><span style="color: #000000;">=</span><span style="color: #000000;"> s; j </span><span style="color: #000000;">=</span><span style="color: #000000;"> s</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 6</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">j</span><span style="color: #000000;">!=</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">) {<br></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">i</span><span style="color: #000000;">!=*</span><span style="color: #000000;">j)<br></span><span style="color: #008080;"> 8</span> <span style="color: #000000;">                </span><span style="color: #000000;">*++</span><span style="color: #000000;">i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">j;<br></span><span style="color: #008080;"> 9</span> <span style="color: #000000;">            j</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">10</span> <span style="color: #000000;">        }<br></span><span style="color: #008080;">11</span> <span style="color: #000000;">        </span><span style="color: #000000;">*++</span><span style="color: #000000;">i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">12</span> <span style="color: #000000;">    }<br></span><span style="color: #008080;">13</span> <span style="color: #000000;">}<br></span><span style="color: #008080;">14</span> <span style="color: #000000;"></span></div> <br><img src ="http://m.shnenglu.com/zerolee/aggbug/142504.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/zerolee/" target="_blank">Zero Lee</a> 2011-03-22 18:40 <a href="http://m.shnenglu.com/zerolee/archive/2011/03/22/142504.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>selection algorithm to select nth small elements based on partitionhttp://m.shnenglu.com/zerolee/archive/2011/03/17/142073.htmlZero LeeZero LeeThu, 17 Mar 2011 12:20:00 GMThttp://m.shnenglu.com/zerolee/archive/2011/03/17/142073.htmlhttp://m.shnenglu.com/zerolee/comments/142073.htmlhttp://m.shnenglu.com/zerolee/archive/2011/03/17/142073.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/142073.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/142073.html1. partition algorithm
 1  function partition(list, left, right, pivotIndex)
 2      pivotValue := list[pivotIndex]
 3      swap list[pivotIndex] and list[right]  // Move pivot to end
 4      storeIndex := left
 5      for i from left to right-1
 6          if list[i] < pivotValue
 7              swap list[storeIndex] and list[i]
 8              storeIndex := storeIndex + 1
 9      swap list[right] and list[storeIndex]  // Move pivot to its final place
10      return storeIndex

2. selection algorithm
 1 function select(list, left, right, k)
 2      loop
 3          select pivotIndex between left and right
 4          pivotNewIndex := partition(list, left, right, pivotIndex)
 5          if k = pivotNewIndex - left + 1
 6              return list[pivotNewIndex]
 7          else if k < pivotNewIndex left + 1
 8              right := pivotNewIndex-1
 9          else
10              left := pivotNewIndex+1
11              k := k pivotNewIndex    

>> In above last statement, one bug. Should be
   k := k-pivotNewIndex-left+1
   left := pivotNewIndex+1

3. code
 1 int partition(std::vector<int>& a, int l, int r)
 2 {
 3     int i = l, j = l;
 4     int p = a[r];
 5     while (j<r) {
 6         if (a[j] < p) {
 7             std::swap(a[i], a[j]);
 8             i++;
 9         }
10         j++;
11     }
12     std::swap(a[r], a[i]);
13     return i;
14 }
15 
16 void select_kth_smallest(std::vector<int>& a, int l, int r, int k)
17 {
18     while (1) {
19         int cut = partition(a, l, r);
20 #if 0
21         std::copy(a.begin()+l, a.begin()+cut, std::ostream_iterator<int>(std::cout, " "));
22         std::cout << " <=" << a[cut] << "=> ";
23         std::copy(a.begin()+cut+1, a.begin()+r+1, std::ostream_iterator<int>(std::cout, " "));
24         std::cout << "cut("<<cut<<"),k("<<k<<"),l("<<l<<"),r("<<r<<")\n";
25 #endif
26         if (cut-l+1==k)
27             break;
28         else if (k < cut-l+1)
29             r = cut-1;
30         else {
31             k = k-cut-1+l;
32             l = cut+1;
33         }
34     }
35 }
36 



Zero Lee 2011-03-17 20:20 鍙戣〃璇勮
]]>
浠庡熬鍒板ご杈撳嚭鍗曢摼琛ㄧ殑鍊?/title><link>http://m.shnenglu.com/zerolee/archive/2011/03/15/141866.html</link><dc:creator>Zero Lee</dc:creator><author>Zero Lee</author><pubDate>Tue, 15 Mar 2011 05:51:00 GMT</pubDate><guid>http://m.shnenglu.com/zerolee/archive/2011/03/15/141866.html</guid><wfw:comment>http://m.shnenglu.com/zerolee/comments/141866.html</wfw:comment><comments>http://m.shnenglu.com/zerolee/archive/2011/03/15/141866.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/zerolee/comments/commentRss/141866.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/zerolee/services/trackbacks/141866.html</trackback:ping><description><![CDATA[棰樼洰錛氳緭鍏ヤ竴涓崟閾捐〃鐨勫ご緇撶偣錛屼粠灝懼埌澶村弽榪囨潵杈撳嚭姣忎釜鑺傜偣鐨勫箋傞摼琛ㄨ妭鐐圭殑瀹氫箟濡備笅錛?br> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span> <span style="color: #0000ff;">struct</span><span style="color: #000000;"> ListNode {<br></span><span style="color: #008080;">2</span> <span style="color: #000000;">   </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> m_nKey;<br></span><span style="color: #008080;">3</span> <span style="color: #000000;">   ListNode</span><span style="color: #000000;">*</span><span style="color: #000000;"> m_pNext;<br></span><span style="color: #008080;">4</span> <span style="color: #000000;">};</span></div> <br>鐞嗚В棰樼洰鐨勬剰鎬濓紝鍙渶瑕佸弽杞緭鍑烘瘡涓嚑鐐圭殑鍊煎氨鍙互浜嗐傚茍涓嶉渶瑕佸皢鍗曢摼琛ㄨ繘琛屽弽杞?br> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> printSingleListReversely(ListNode</span><span style="color: #000000;">*</span><span style="color: #000000;"> phead) <br></span><span style="color: #008080;">2</span> <span style="color: #000000;">{<br></span><span style="color: #008080;">3</span> <span style="color: #000000;">   </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">phead</span><span style="color: #000000;">!=</span><span style="color: #000000;">NULL) {<br></span><span style="color: #008080;">4</span> <span style="color: #000000;">       printStringListReversely(phead</span><span style="color: #000000;">-></span><span style="color: #000000;">m_pNext);<br></span><span style="color: #008080;">5</span> <span style="color: #000000;">       print(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d </span><span style="color: #000000;">"</span><span style="color: #000000;">, phead</span><span style="color: #000000;">-></span><span style="color: #000000;">m_nKey);<br></span><span style="color: #008080;">6</span> <span style="color: #000000;">   }<br></span><span style="color: #008080;">7</span> <span style="color: #000000;">}</span></div> <br>寤朵幾錛?) 浠庡熬鍒板ご杈撳嚭瀛楃涓? 2錛夊畾涔変釜鍑芥暟姹傚瓧絎︿覆鐨勯暱搴︼紝鍑芥暟鍐呬笉鑳藉0鏄庝換浣曞彉閲忋?br><br><img src ="http://m.shnenglu.com/zerolee/aggbug/141866.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/zerolee/" target="_blank">Zero Lee</a> 2011-03-15 13:51 <a href="http://m.shnenglu.com/zerolee/archive/2011/03/15/141866.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鑵捐闈㈣瘯棰?/title><link>http://m.shnenglu.com/zerolee/archive/2011/03/11/141588.html</link><dc:creator>Zero Lee</dc:creator><author>Zero Lee</author><pubDate>Fri, 11 Mar 2011 11:20:00 GMT</pubDate><guid>http://m.shnenglu.com/zerolee/archive/2011/03/11/141588.html</guid><wfw:comment>http://m.shnenglu.com/zerolee/comments/141588.html</wfw:comment><comments>http://m.shnenglu.com/zerolee/archive/2011/03/11/141588.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/zerolee/comments/commentRss/141588.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/zerolee/services/trackbacks/141588.html</trackback:ping><description><![CDATA[涓涓枃浠朵腑鏈?0浜夸釜鏁存暟錛屾瘡涓暣鏁頒負鍥涗釜瀛楄妭錛屽唴瀛樹負1GB錛屽啓鍑轟竴涓畻娉曪細姹傚嚭榪欎釜鏂囦歡閲岀殑鏁存暟閲屼笉鍖呭惈鐨勪竴涓暣鏁?br>涓嬮潰鐨勪唬鐮佺墖孌典粎浠呮槸涓涓牱渚嬨?br>4涓瓧鑺傜殑鏁存暟鏈澶у彲琛ㄧず涓?^32=4294967296, 涓涓暟涓涓暟鐨勮鍏ュ唴瀛橈紝寤虹珛涓涓猙it map錛屽叡闇瑕?294967296涓猙its(涔熷氨鏄?.5G瀛楄妭鐨勫唴瀛橈紝騫舵病鏈夎秴榪?G鍐呭瓨鐨勯檺鍒?錛岃鍏ユ瘡涓涓暟錛岀疆鐩稿簲鐨刡it涓?銆?br> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;"> 1</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> N </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">20</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> # of number</span><span style="color: #008000;"><br></span><span style="color: #008080;"> 2</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> M </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">1000</span><span style="color: #000000;">;   </span><span style="color: #008000;">//</span><span style="color: #008000;"> number range</span><span style="color: #008000;"><br></span><span style="color: #008080;"> 3</span> <span style="color: #008000;"></span><span style="color: #000000;">    std::vector</span><span style="color: #000000;"><</span><span style="color: #0000ff;">int</span><span style="color: #000000;">></span><span style="color: #000000;"> a(N);  // can be imported from external file number by number<br></span><span style="color: #008080;"> 4</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> N; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;"> 5</span> <span style="color: #000000;">        a[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)rand()</span><span style="color: #000000;">%</span><span style="color: #000000;">M;<br></span><span style="color: #008080;"> 6</span> <span style="color: #000000;">    std::copy(a.begin(), a.end(), std::ostream_iterator</span><span style="color: #000000;"><</span><span style="color: #0000ff;">int</span><span style="color: #000000;">></span><span style="color: #000000;">(std::cout, </span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">));<br></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 8</span>     <span style="color: #000000;">// bit map setup for existence of each number<br></span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    unsigned </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> nbytes </span><span style="color: #000000;">=</span><span style="color: #000000;"> M</span><span style="color: #000000;">%</span><span style="color: #000000;">8</span><span style="color: #000000;"> </span><span style="color: #000000;">?</span><span style="color: #000000;"> (M</span><span style="color: #000000;">/</span><span style="color: #000000;">8</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">) : (M</span><span style="color: #000000;">/</span><span style="color: #000000;">8</span><span style="color: #000000;">);<br></span><span style="color: #008080;">10</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">nbytes = </span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;"><<</span><span style="color: #000000;"> nbytes </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span> <span style="color: #000000;"><br></span><span style="color: #008080;">12</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> p </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> [nbytes];<br></span><span style="color: #008080;">13</span> <span style="color: #000000;">    memset(p, </span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">nbytes);<br></span><span style="color: #008080;">14</span> <span style="color: #000000;"><br></span><span style="color: #008080;">15</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> N; i</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br></span><span style="color: #008080;">16</span> <span style="color: #000000;">        unsigned </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> index </span><span style="color: #000000;">=</span><span style="color: #000000;"> a[i]</span><span style="color: #000000;">/</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br></span><span style="color: #008080;">17</span> <span style="color: #000000;">        unsigned </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> bitpos </span><span style="color: #000000;">=</span><span style="color: #000000;"> a[i]</span><span style="color: #000000;">%</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br></span><span style="color: #008080;">18</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> tmp </span><span style="color: #000000;">=</span><span style="color: #000000;"> p</span><span style="color: #000000;">+</span><span style="color: #000000;">index;<br></span><span style="color: #008080;">19</span> <span style="color: #000000;">        </span><span style="color: #000000;">*</span><span style="color: #000000;">tmp </span><span style="color: #000000;">|=</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;"> </span><span style="color: #000000;"><<</span><span style="color: #000000;"> bitpos;<br></span><span style="color: #008080;">20</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;">std::cout << "bit pos set to 1 : " << 8*index+bitpos << "\n";</span><span style="color: #008000;"><br></span><span style="color: #008080;">21</span> <span style="color: #008000;"></span><span style="color: #000000;">    }<br></span><span style="color: #008080;">22</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> nbytes</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">; i </span><span style="color: #000000;">>=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i</span><span style="color: #000000;">--</span><span style="color: #000000;">) {<br></span><span style="color: #008080;">23</span> <span style="color: #000000;">        printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%02X </span><span style="color: #000000;">"</span><span style="color: #000000;">, (</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">(p</span><span style="color: #000000;">+</span><span style="color: #000000;">i)</span><span style="color: #000000;">&</span><span style="color: #000000;">0xFF</span><span style="color: #000000;">);<br></span><span style="color: #008080;">24</span> <span style="color: #000000;">    }<br></span><span style="color: #008080;">25</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">26</span> <span style="color: #000000;">    delete [] p;<br></span><span style="color: #008080;">27</span> <span style="color: #000000;"></span></div> <br> <img src ="http://m.shnenglu.com/zerolee/aggbug/141588.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/zerolee/" target="_blank">Zero Lee</a> 2011-03-11 19:20 <a href="http://m.shnenglu.com/zerolee/archive/2011/03/11/141588.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>闈瀙rintf褰㈠紡鐨勫崄鍏繘鍒跺拰浜岃繘鍒舵墦鍗幫紙闆呰檸闈㈣瘯棰橈級http://m.shnenglu.com/zerolee/archive/2011/03/11/141586.htmlZero LeeZero LeeFri, 11 Mar 2011 11:14:00 GMThttp://m.shnenglu.com/zerolee/archive/2011/03/11/141586.htmlhttp://m.shnenglu.com/zerolee/comments/141586.htmlhttp://m.shnenglu.com/zerolee/archive/2011/03/11/141586.html#Feedback2http://m.shnenglu.com/zerolee/comments/commentRss/141586.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/141586.html
 1 template <typename T>
 2 void displayHexBin(const T& v)
 3 {
 4     const unsigned char c2h[] = "0123456789ABCDEF";
 5     const unsigned char c2b[] = "01";
 6 
 7     unsigned char* p = (unsigned char*)&v;
 8     char* buf = new char [sizeof(T)*2+1];
 9     char* ptmp = buf;
10     p = p + sizeof(T)-1;
11     for (int i = 0; i < sizeof(T); i++--p) {
12         *buf++ = c2h[*>> 4];
13         *buf++ = c2h[*& 0x0F];
14     }
15     *buf = '\0';
16     printf("hex format displayed as %s\n", ptmp);
17 
18     delete [] ptmp;
19     p = (unsigned char*)&v; p = p + sizeof(T)-1;
20     ptmp = buf = new char [sizeof(T)*8+1];
21     for (int i = 0; i < sizeof(T); i++--p) {
22         for (int j = 0; j < 8; j++)
23             *buf++ = c2b[(*>> (7-j)) & 0x1];
24     }
25     *buf = '\0';
26     printf("bin format displayed as %s\n", ptmp);
27     delete [] ptmp;
28 }
29 




Zero Lee 2011-03-11 19:14 鍙戣〃璇勮
]]>
鐧懼害鐨勯潰璇曢瑙g瓟http://m.shnenglu.com/zerolee/archive/2011/03/11/141584.htmlZero LeeZero LeeFri, 11 Mar 2011 11:10:00 GMThttp://m.shnenglu.com/zerolee/archive/2011/03/11/141584.htmlhttp://m.shnenglu.com/zerolee/comments/141584.htmlhttp://m.shnenglu.com/zerolee/archive/2011/03/11/141584.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/141584.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/141584.html瑕佹眰錛氱┖闂村鏉傚害O(1)錛屾椂闂村鏉傚害涓篛錛坣錛夈?br>
 1 bool func(int n)
 2 {
 3     return (n&1)==0; // n%2 is more expensive
 4 }
 5 
 6 void group_oddeven(std::vector<int>& a, bool (*func)(int))
 7 {
 8     int i = 0, j = a.size()-1;
 9     int buf = 0;
10     while (i < j) {
11         if (!func(a[i]))    // odd, move forward
12         {   i++continue; }
13         if (func(a[j]))     // even, move backward
14         {   j--continue; }
15 
16         std::swap(a[i++], a[j--]);
17     }
18 }
19 



Zero Lee 2011-03-11 19:10 鍙戣〃璇勮
]]>
璁捐鍖呭惈min鍑芥暟鐨勬爤http://m.shnenglu.com/zerolee/archive/2010/11/26/134718.htmlZero LeeZero LeeFri, 26 Nov 2010 04:13:00 GMThttp://m.shnenglu.com/zerolee/archive/2010/11/26/134718.html瑕佹眰鍑芥暟min, push, pop鐨勬椂闂村鏉傚害閮芥槸O(1)
 1 class stackmin {
 2     int data[N];
 3     int minidx[N];
 4     int top;
 5 public:
 6     stackmin()
 7         : top(-1)
 8     {
 9         ::memset(minidx, -1sizeof(int)*N);
10         ::memset(data, 0sizeof(int)*N);
11     }
12 
13     bool push(int d)
14     {
15         if (top < N-1)
16             data[++top] = d;
17         else
18             return false;
19         if (top==0)
20             minidx[top] = 0;
21         else if (data[minidx[top-1]] > d)
22             minidx[top] = top;
23         else
24             minidx[top] = minidx[top-1];
25         return true;
26     }
27     bool pop(int& d)
28     {
29         if (top<0)
30             return false;
31         else
32 
33             d = data[top];
34         minidx[top] = -1;
35         top--;
36         return true;
37     }
38     bool min(int& m)
39     {
40         if (top>=0)
41             m = data[minidx[top]];
42         else
43             return false;
44         return true;
45     }
46     void print()
47     {
48         for (int i = 0; i <= top; i++)
49             std::cout << data[i] << " ";
50         std::cout << "\n";
51     }
52 };
53 

鍊熷姪鍙︿竴涓唴閮ㄧ殑鏁扮粍錛岀敤鏉ュ瓨鍌ㄦ瘡嬈ush鍏冪礌涔嬪悗鐨勬爤鏈灝忓肩殑绱㈠紩錛屼究鍙緢鏂逛究鐨勫湪O(1)鐨勬椂闂村唴瀹屾垚浠ヤ笂涓夌鎿嶄綔銆備竴縐嶇┖闂存崲鏃墮棿鐨勬柟娉曘?br>



Zero Lee 2010-11-26 12:13 鍙戣〃璇勮
]]>
Calcuate power n of x recursivelyhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130297.htmlZero LeeZero LeeMon, 18 Oct 2010 09:19:00 GMThttp://m.shnenglu.com/zerolee/archive/2010/10/18/130297.htmlhttp://m.shnenglu.com/zerolee/comments/130297.htmlhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130297.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/130297.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/130297.htmlGiven x and n, calculate its power n:
 1 int power(int x, int n)
 2 {
 3    if (n==0
 4      return 1;
 5    else (n%2==0
 6      return power(x*x, n/2);
 7    else 
 8      return x*power(x*x, n/2);
 9 }
10   
11




Zero Lee 2010-10-18 17:19 鍙戣〃璇勮
]]>
Convert one binary search tree to double-linked listhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130252.htmlZero LeeZero LeeMon, 18 Oct 2010 04:30:00 GMThttp://m.shnenglu.com/zerolee/archive/2010/10/18/130252.htmlhttp://m.shnenglu.com/zerolee/comments/130252.htmlhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130252.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/130252.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/130252.htmlCpp code demo as below:
 1 struct BSTreeNode
 2 {
 3     int m_value;
 4     BSTreeNode* m_pLeft;
 5     BSTreeNode* m_pRight;
 6 };
 7 
 8 BSTreeNode* convert(BSTreeNode* phead, bool asRight)
 9 {
10     if (!phead)
11         return NULL;
12     BSTreeNode* pLeft = 0*pRight=0;
13     if (phead->m_pLeft)
14         pLeft = convert(phead->m_pLeft, false);
15     if (pLeft) {
16         pLeft->m_pRight = phead;
17         phead->m_pLeft = pLeft;
18     }
19     if (phead->m_pRight)
20         pRight = convert(phead->m_pRight, true);
21     if (pRight) {
22         pRight->m_pLeft = phead;
23         phead->m_pRight = pRight;
24     }
25     BSTreeNode* ptmp = phead;
26     if (asRight) {
27         while (ptmp->m_pLeft) {
28             ptmp = ptmp->m_pLeft;
29         }
30     } else {
31         while (ptmp->m_pRight) {
32             ptmp = ptmp->m_pRight;
33         }
34     }
35     return ptmp;
36 }
37 
38 BSTreeNode* convert2doublelist(BSTreeNode* phead)
39 {
40     return convert(phead, true);
41 }
42 




Zero Lee 2010-10-18 12:30 鍙戣〃璇勮
]]>
Calculate maximum sum of any subarray sethttp://m.shnenglu.com/zerolee/archive/2010/10/18/130251.htmlZero LeeZero LeeMon, 18 Oct 2010 04:27:00 GMThttp://m.shnenglu.com/zerolee/archive/2010/10/18/130251.htmlhttp://m.shnenglu.com/zerolee/comments/130251.htmlhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130251.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/130251.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/130251.htmlCpp code demo as below:
 1 int maxSubarraySum(int arr[], int length, int& beg, int& end)
 2 {
 3     assert(arr!=0);
 4     int maxSum = 0;
 5     int tmpSum = 0;
 6     beg = 0; end = 0;
 7     for (int i = 0; i < length; i++) {
 8         tmpSum += arr[i];
 9         if (tmpSum < 0) {   // discard it
10             tmpSum = 0;
11             beg = end = i+1;
12         }
13         if (tmpSum > maxSum) {
14             maxSum = tmpSum;
15             end = i+1;
16         }
17     }
18     if (maxSum ==0 ) {      // if all numbers are negative, return max value
19         maxSum = arr[0];
20         for (int i = 1; i < length; i++)
21             if (maxSum < arr[i]) {
22                 maxSum = arr[i];
23                 beg = i; end = i+1;
24             }
25     }
26     return maxSum;
27 }
28 




Zero Lee 2010-10-18 12:27 鍙戣〃璇勮
]]>
One SRC demo to reverse one single-linked listhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130248.htmlZero LeeZero LeeMon, 18 Oct 2010 04:23:00 GMThttp://m.shnenglu.com/zerolee/archive/2010/10/18/130248.htmlhttp://m.shnenglu.com/zerolee/comments/130248.htmlhttp://m.shnenglu.com/zerolee/archive/2010/10/18/130248.html#Feedback0http://m.shnenglu.com/zerolee/comments/commentRss/130248.htmlhttp://m.shnenglu.com/zerolee/services/trackbacks/130248.html
 1 struct listNode {
 2    int value;
 3    listNode* next;
 4 };
 5 
 6 listNode* reverseList(listNode* phead)
 7 {
 8     listNode* pcur = phead;
 9     listNode* pprev = 0*pnext = 0;
10     while (pcur) {
11       pnext = pcur->next;
12       pcur->next = pprev;
13 
14       pprev = pcur;
15       pcur = pnext;
16    }
17    return pcur;
18 }




Zero Lee 2010-10-18 12:23 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美第一黄网免费网站| 欧美国产三级| 欧美一区二区在线播放| 性娇小13――14欧美| 香蕉av福利精品导航| 久久国产主播| 久久亚洲捆绑美女| 欧美gay视频激情| 亚洲第一精品福利| 亚洲第一网站| 日韩亚洲不卡在线| 亚洲五月婷婷| 欧美一区二区在线免费观看| 欧美在线视屏| 欧美不卡一区| 欧美日本在线视频| 国产精品毛片一区二区三区| 国产三级精品在线不卡| 国产综合久久久久久| 亚洲激情视频在线播放| 夜夜嗨av一区二区三区四区 | 亚洲国产成人午夜在线一区| 91久久香蕉国产日韩欧美9色| 99re6热只有精品免费观看| 亚洲图片欧美一区| 久久精视频免费在线久久完整在线看| 久久免费少妇高潮久久精品99| 欧美国产精品久久| 国产精品xxxav免费视频| 国语精品中文字幕| 亚洲美女av黄| 欧美一区二区三区免费在线看| 久久综合五月| 99国产精品久久久久久久| 午夜精品久久久久久久久| 嫩草伊人久久精品少妇av杨幂| 欧美日韩在线视频首页| 国产在线欧美日韩| 亚洲视频在线观看一区| 久久久五月天| 99ri日韩精品视频| 久久精品成人一区二区三区蜜臀 | 久久精品国产在热久久| 亚洲视频每日更新| 国产专区综合网| 亚洲国产小视频| 亚洲一区二区三区激情| 亚洲一区二区三区在线观看视频| 久久国内精品自在自线400部| av72成人在线| 欧美激情精品久久久久久免费印度| 黄色影院成人| 久久综合狠狠综合久久激情| 亚洲你懂的在线视频| 国产精品成人观看视频免费| 99精品免费视频| 亚洲国产清纯| 欧美成人午夜激情视频| 亚洲国产美国国产综合一区二区| 久久躁日日躁aaaaxxxx| 久久av一区| 国产综合网站| 久久免费精品视频| 久久九九国产| 极品尤物久久久av免费看| 久热re这里精品视频在线6| 久久超碰97人人做人人爱| 国产亚洲综合精品| 久久久99国产精品免费| 欧美一区二区久久久| 国产私拍一区| 猛干欧美女孩| 欧美成人性网| 亚洲视频精选在线| 一本综合久久| 国产精品爽爽爽| 久久精品一区二区| 久久久精品动漫| 在线日韩视频| 最新国产成人在线观看| 欧美日韩国产片| 亚洲一区二区视频在线| 一本一本a久久| 国产区日韩欧美| 老司机亚洲精品| 欧美国产高清| 亚洲一区二区精品| 午夜国产精品影院在线观看 | 亚洲欧美电影在线观看| 国产三级精品三级| 欧美 日韩 国产在线| 欧美国产精品中文字幕| 亚洲免费在线观看视频| 欧美一区二区三区视频| 曰本成人黄色| 亚洲精品国产精品国自产观看| 国产精品福利av| 久久久久久久尹人综合网亚洲| 蜜桃精品久久久久久久免费影院| 99国内精品久久| 亚洲在线观看免费| 亚洲福利视频一区| 99精品免费视频| 国内外成人免费激情在线视频网站 | 亚洲影院免费| 久久九九全国免费精品观看| 亚洲第一在线| 在线视频欧美日韩| 在线精品国产成人综合| 日韩视频在线永久播放| 国产日韩欧美亚洲| 亚洲精品1区| 国产亚洲精品bv在线观看| 亚洲国产精品久久久| 国产美女搞久久| 亚洲高清色综合| 国产日韩欧美在线一区| 亚洲青涩在线| 国产伊人精品| 一区二区高清视频| 亚洲盗摄视频| 亚洲欧美国产精品va在线观看| 亚洲国产网站| 欧美亚洲一区在线| 国产精品99久久久久久www| 久久精品主播| 性做久久久久久免费观看欧美| 欧美电影打屁股sp| 久久国内精品视频| 欧美三级特黄| 欧美18av| 国产在线高清精品| 一区二区欧美精品| 亚洲人成网站精品片在线观看| 亚洲你懂的在线视频| 99精品视频免费观看视频| 久久人人爽人人爽| 久久国产福利国产秒拍| 欧美三日本三级三级在线播放| 欧美激情在线观看| 国产一区二区高清不卡| 中国日韩欧美久久久久久久久| 最新中文字幕亚洲| 久久久成人精品| 欧美在线视频网站| 欧美午夜精品| 亚洲精品一区二区三区婷婷月| 亚洲福利av| 久久不射中文字幕| 久久精品亚洲乱码伦伦中文 | 欧美在线视频观看免费网站| 欧美三级电影一区| 亚洲日本电影在线| 亚洲精品永久免费| 欧美3dxxxxhd| 欧美jizzhd精品欧美喷水| 韩国三级在线一区| 新狼窝色av性久久久久久| 午夜精品免费| 国产精品欧美经典| 宅男精品视频| 亚洲女人小视频在线观看| 欧美日韩喷水| 99精品国产在热久久| 一区二区三区 在线观看视频| 欧美aa国产视频| 欧美大片网址| 亚洲欧洲精品一区二区| 久久琪琪电影院| 你懂的视频欧美| 亚洲高清在线视频| 久久综合中文| 欧美激情精品久久久久久黑人 | 亚洲欧洲一区二区天堂久久| 最新精品在线| 欧美成人国产va精品日本一级| 亚洲高清三级视频| 亚洲精品一二三区| 欧美精品久久久久a| 亚洲美女在线看| 亚洲一级二级在线| 国产精品美女久久| 午夜天堂精品久久久久| 久久精品国产v日韩v亚洲| 国产亚洲a∨片在线观看| 欧美一区二区三区视频在线观看| 久久精品91久久香蕉加勒比 | 欧美成人高清视频| 亚洲人妖在线| 午夜精品福利在线观看| 国产精品日本精品| 亚洲免费影院| 久久亚洲电影| 亚洲电影成人| 欧美久久久久久蜜桃| 日韩视频一区二区三区在线播放| 亚洲女人av| 狠狠v欧美v日韩v亚洲ⅴ| 美女性感视频久久久| 亚洲全部视频|