﻿<?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>BlogJava-&lt;font size="6"&gt;Ytl's Java Blog&lt;/font&gt;-随笔分类-Algorithms and programming concepts</title><link>http://www.blogjava.net/ytl-zlq/category/48476.html</link><description>&lt;font size="4" &gt;厚积而薄发---每一天都是一个全新的开始&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Thu, 01 Nov 2012 11:30:05 GMT</lastBuildDate><pubDate>Thu, 01 Nov 2012 11:30:05 GMT</pubDate><ttl>60</ttl><item><title>Binary search algorithm</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349702.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Fri, 06 May 2011 10:11:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349702.html</guid><description><![CDATA[<table style="background-color: #f5f5f5; margin-left: auto; margin-right: auto; ">
    <tbody>
        <tr>
            <td>
            <div class="width" style="min-width: 960px; width: 960px; margin-left: auto; margin-right: auto; ">
            <div class="padding">
            <div class="content" style="padding-right: 25px; padding-bottom: 25px; padding-left: 25px; padding-top: 0px; margin-bottom: 2px; vertical-align: top; width: 739px; margin-right: -1px; border-left-color: #dddddd; background-color: #ffffff; ">
            <div class="inside" style="margin-top: 16px; ">
            <h1 style="font-size: 18pt; ">Binary search algorithm</h1>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Generally, to find a value in unsorted array, we should look through elements of an array one by one, until searched value is found. In case&nbsp;of searched value is absent from array, we go through all elements. In average, complexity of such an algorithm is proportional to the length of the array.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Situation changes significantly, when array is sorted. If we know it, random access capability can be utilized very&nbsp;<span style="font-family: 'Times New Roman'; font-size: 12pt; ">efficiently</span>to find searched value quick. Cost of searching algorithm reduces to binary logarithm of the array length. For reference, log<sub>2</sub>(1 000 000) ≈ 20. It means, that&nbsp;<strong>in worst case</strong>, algorithm makes 20 steps to find a value in sorted array of a million elements or to say, that it doesn't present it the array.</p>
            <h2 style="font-size: 14pt; ">Algorithm</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Algorithm is quite simple. It can be done either recursively or iteratively:</p>
            <ol>
                <li>get the middle element;</li>
                <li>if the middle element equals to the searched value, the algorithm stops;</li>
                <li>otherwise, two cases are possible:
                <ul>
                    <li>searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.</li>
                    <li>searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.</li>
                </ul>
                </li>
            </ol>
            Now we should define, when iterations should stop. First case is when searched element is found. Second one is when subarray has no elements. In this case, we can conclude, that searched value doesn't present in the array.
            <h2 style="font-size: 14pt; ">Examples</h2>
            <p class="style4" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Times New Roman', Times, serif; "><em>Example 1.&nbsp;</em>Find 6 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.</p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 1 (middle element is 19 &gt; 6):</span>&nbsp;&nbsp;&nbsp;<font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font>&nbsp;-1&nbsp; 5&nbsp; 6&nbsp; 18&nbsp;&nbsp;<span class="style10" style="color: #cc0033; font-weight: bold; ">19</span>&nbsp; 25&nbsp; 46&nbsp; 78&nbsp; 102&nbsp; 114</font></p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 2 (middle element is 5 &lt; 6):</span>&nbsp;&nbsp;&nbsp;&nbsp;<font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font>&nbsp;-1&nbsp;&nbsp;<span class="style10" style="color: #cc0033; font-weight: bold; ">5</span>&nbsp; 6&nbsp; 18&nbsp;&nbsp;<span class="style7" style="color: #cccccc; ">19&nbsp; 25&nbsp; 46&nbsp; 78&nbsp; 102&nbsp; 114</span></font></p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 3 (middle element is 6 == 6):</span>&nbsp;&nbsp;&nbsp;<font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font>&nbsp;<span class="style7" style="color: #cccccc; ">-1&nbsp; 5</span>&nbsp;&nbsp;<span class="style8" style="color: #009933; font-weight: bold; ">6</span>&nbsp; 18&nbsp;&nbsp;<span class="style7" style="color: #cccccc; ">19&nbsp; 25&nbsp; 46&nbsp; 78&nbsp; 102&nbsp; 114</span></font></font></p>
            <p class="style4" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Times New Roman', Times, serif; "><em>Example 2.&nbsp;</em>Find 103 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.</p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 1 (middle element is 19 &lt; 103):</span>&nbsp;&nbsp;<font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font>-1&nbsp; 5&nbsp; 6&nbsp; 18&nbsp;&nbsp;<span class="style10" style="color: #cc0033; font-weight: bold; ">19</span>&nbsp; 25&nbsp; 46&nbsp; 78&nbsp; 102&nbsp; 114</font></p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 2 (middle element is 78 &lt; 103):</span>&nbsp;&nbsp;<font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font><span class="style7" style="color: #cccccc; ">-1&nbsp; 5&nbsp; 6&nbsp; 18&nbsp; 19</span><span class="style13" style="color: #cccccc; ">&nbsp;</span><span class="style14" style="color: #000000; ">&nbsp;25&nbsp; 46&nbsp;&nbsp;<span class="style10" style="color: #cc0033; font-weight: bold; ">78</span>&nbsp; 102&nbsp; 114</span></font></p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 3 (middle element is 102 &lt; 103):</span><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font><span class="style13" style="color: #cccccc; ">-1&nbsp; 5&nbsp; 6&nbsp; 18&nbsp; 19&nbsp; 25&nbsp; 46&nbsp; 78</span><span class="style7" style="color: #cccccc; ">&nbsp;&nbsp;<span class="style14" style="color: #000000; "><span class="style10" style="color: #cc0033; font-weight: bold; ">102</span>&nbsp; 114</span></span></font></font></p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 4 (middle element is 114 &gt; 103):</span><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font><span class="style13" style="color: #cccccc; ">-1&nbsp; 5&nbsp; 6&nbsp; 18&nbsp; 19&nbsp; 25&nbsp; 46&nbsp; 78</span><span class="style13" style="color: #cccccc; ">&nbsp;&nbsp;<span class="style14" style="color: #000000; "><span class="style13" style="color: #cccccc; ">102</span>&nbsp;&nbsp;<span class="style10" style="color: #cc0033; font-weight: bold; ">114</span></span></span></font></font></p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; "><font><font><span class="style18" style="color: #9966ff; font-size: 11pt; ">Step 5 (searched value is absent):</span><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font><font><span class="style13" style="color: #cccccc; ">&nbsp;</span><font><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font></font><font><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font></font><font><font><span class="style13" style="color: #cccccc; ">&nbsp;</span></font></font></font><span class="style13" style="color: #cccccc; ">-1&nbsp; 5&nbsp; 6&nbsp; 18&nbsp; 19&nbsp; 25&nbsp; 46&nbsp; 78</span><span class="style13" style="color: #cccccc; ">&nbsp; 102&nbsp; 114</span></font><br />
            </font></p>
            <h2 style="font-size: 14pt; ">Complexity analysis</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Huge advantage of this algorithm is that it's complexity depends on the array size logarithmically&nbsp;<strong>in worst case.&nbsp;</strong>In practice it means, that algorithm will do at most log<sub>2</sub>(n) iterations, which is a very small number even for big arrays. It can be proved very easily. Indeed, on every step the size of the searched part is reduced by half. Algorithm stops, when there are no elements to search in. Therefore, solving following inequality in whole numbers:</p>
            <p class="style3" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Courier New', Courier, monospace; ">n / 2<sup>iterations</sup>&nbsp;&gt; 0</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">resulting in</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><span class="style3" style="font-family: 'Courier New', Courier, monospace; ">iterations &lt;= log<sub>2</sub>(n).</span></p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">It means, that binary search algorithm time complexity is&nbsp;<span class="style3" style="font-family: 'Courier New', Courier, monospace; ">O(log<sub>2</sub>(n)).</span></p>
            <h2 style="font-size: 14pt; ">Code snippets.</h2>
            You can see recursive solution for Java and iterative for python below.
            <h3 style="font-size: 12pt; ">Java</h3>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;binarySearch(</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">[] array,&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;value,&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;left,&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;right) {</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">if</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(left &gt; right)</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">return</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;-1;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;middle = left + (right-left) / 2;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">if</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(array[middle] == value)</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">return</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;middle;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">if</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(array[middle] &gt; value)</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">return</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;binarySearch(array, value, left, middle - 1);</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">else</span></strong></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">return</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;binarySearch(array, value, middle + 1, right);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">}</span></p>
            <h3 style="font-size: 12pt; ">Python</h3>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: blue; ">def</span><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp;<span style="color: #010001; ">biSearch</span>(<font color="#0000ff">L,e,first,last</font>):</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp; if last - first &lt; 2: return L[first] == e or L[last] == e<br />
            </span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp; mid = first + (last-first)/2</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp; if L[mid] ==e: return True</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp; if L[mid]&gt; e :&nbsp;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return biSearch(L,e,first,mid-1)</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp; return biSearch(L,e,mid+1,last)</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp; &nbsp; &nbsp;&nbsp;</span></p>
            </div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/ytl-zlq/aggbug/349702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ytl-zlq/" target="_blank">ytl</a> 2011-05-06 18:11 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349702.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Algorithm to merge sorte</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349695.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Fri, 06 May 2011 09:05:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349695.html</guid><description><![CDATA[<span style="font-size: 13px; line-height: 19px; font-family: sans-serif; "><strong></strong></span><span style="font-size: 12pt; "><span style="font-size: 13px; line-height: 19px; font-family: sans-serif; "><strong></strong></span><span style="font-size: 18pt; "><span style="font-size: 13px; line-height: 19px; font-family: sans-serif; "><strong>Merge sort</strong>&nbsp;is an&nbsp;<em><a href="http://en.wikipedia.org/wiki/Big_O_notation" title="Big O notation" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">O</a></em>(<em>n</em>&nbsp;log&nbsp;<em>n</em>)&nbsp;<a href="http://en.wikipedia.org/wiki/Comparison_sort" title="Comparison sort" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">comparison-based</a>&nbsp;<a href="http://en.wikipedia.org/wiki/Sorting_algorithm" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">sorting algorithm</a>. Most implementations produce a&nbsp;<a href="http://en.wikipedia.org/wiki/Sorting_algorithm#Classification" title="Sorting algorithm" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">stable sort</a>, meaning that the implementation preserves the input order of equal elements in the sorted output. It is a&nbsp;<a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">divide and conquer algorithm</a>. Merge sort was invented by&nbsp;<a href="http://en.wikipedia.org/wiki/John_von_Neumann" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">John von Neumann</a>&nbsp;in 1945.</span><span style="font-size: 13px; line-height: 19px; font-family: sans-serif; ">&nbsp;A detailed description and analysis of bottom-up mergesort appeared in a report by<a href="http://en.wikipedia.org/wiki/Herman_Goldstine" title="Herman Goldstine" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">Goldstine</a>&nbsp;and Neumann as early as 1948</span></span></span>
<div><span style="font-family: sans-serif; font-size: 13px; line-height: 19px; ">&nbsp;<a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" style="color: #0645ad; text-decoration: none; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">divide and conquer algorithm</a></span><span style="font-family: sans-serif; font-size: 13px; line-height: 19px; ">: 1, split the problem into several subproblem of the same type. 2,solove independetly. 3 combine those solutions<br />
<br />
<br />
</span></div>
<div><font face="sans-serif" size="2"><span style="line-height: 19px;"><br />
</span></font></div>
<div>Python Implement<br />
&nbsp;&nbsp;<br />
&nbsp; def mergeSort(L):<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if len(L) &lt; 2 :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &nbsp;L<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;middle = len(L)/2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = mergeSort(L[:mddle])<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = mergeSort(L[middle:])<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;together = merge(left,right)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return together</div>
<img src ="http://www.blogjava.net/ytl-zlq/aggbug/349695.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ytl-zlq/" target="_blank">ytl</a> 2011-05-06 17:05 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349695.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Algorithm to merge sorted arrays</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349692.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Fri, 06 May 2011 08:55:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349692.html</guid><description><![CDATA[<table style="background-color: #f5f5f5; margin-left: auto; margin-right: auto; ">
    <tbody>
        <tr>
            <td>
            <div class="width" style="min-width: 960px; width: 960px; margin-left: auto; margin-right: auto; ">
            <div class="padding">
            <div class="content" style="padding: 0px 25px 25px; margin-bottom: 2px; vertical-align: top; width: 739px; margin-right: -1px; border-left-color: #dddddd; background-color: #ffffff;">
            <div class="inside" style="margin-top: 16px; ">
            <h1 style="font-size: 18pt; ">Algorithm to merge sorted arrays</h1>
            <p style="text-align: justify; letter-spacing: 0.2px; ">In the article we present an algorithm for merging two sorted arrays. One can learn how to operate with several arrays and master read/write indices. Also, the algorithm has certain applications in practice, for instance in merge sort.</p>
            <h2 style="font-size: 14pt; ">Merge algorithm</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Assume, that both arrays are sorted in ascending order and we want resulting array to maintain the same order. Algorithm to merge two arrays&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">A[0..m-1]</span>&nbsp;and&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">B[0..n-1]</span>&nbsp;into an array&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">C[0..m+n-1]</span>&nbsp;is as following:</p>
            <ol>
                <li>Introduce read-indices&nbsp;<strong>i</strong>,&nbsp;<strong>j</strong>&nbsp;to traverse arrays&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">A</span>&nbsp;and&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">B</span>, accordingly. Introduce write-index&nbsp;<strong>k</strong>&nbsp;to store position of the first free cell in the resulting array. By default&nbsp;<strong>i</strong>&nbsp;=&nbsp;<strong>j</strong>&nbsp;=&nbsp;<strong>k</strong>&nbsp;= 0.</li>
                <li>At each step: if both indices are in range (<strong>i</strong>&nbsp;&lt; m and&nbsp;<strong>j</strong>&nbsp;&lt; n), choose minimum of&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">(A[<strong>i</strong>], B[<strong>j</strong>])</span>&nbsp;and write it to<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">C[<strong>k</strong>]</span>. Otherwise go to step 4.</li>
                <li>Increase&nbsp;<strong>k</strong>&nbsp;and index of the array, algorithm located minimal value at, by one. Repeat step 2.</li>
                <li>Copy the rest values from the array, which index is still in range, to the resulting array.</li>
            </ol>
            <h2 style="font-size: 14pt; ">Enhancements</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Algorithm could be enhanced in many ways. For instance, it is reasonable to check, if&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">A[m&nbsp;-&nbsp;1]&nbsp;&lt;&nbsp;B[0]</span>&nbsp;or<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">B[n&nbsp;-&nbsp;1]&nbsp;&lt;&nbsp;A[0]</span>. In any of those cases, there is no need to do more comparisons. Algorithm could just copy source arrays in the resulting one in the right order. More complicated enhancements may include searching for interleaving parts and run merge algorithm for them only. It could save up much time, when sizes of merged arrays differ in scores of times.</p>
            <h2 style="font-size: 14pt; ">Complexity analysis</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Merge algorithm's time complexity is&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">O(n + m).</span>&nbsp;Additionally, it requires&nbsp;<span class="style1" style="font-family: 'Courier New', Courier, monospace; ">O(n + m)</span>&nbsp;additional space to store resulting array.</p>
            <h2 style="font-size: 14pt; ">Code snippets</h2>
            <h3 style="font-size: 12pt; ">Java implementation</h3>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: #3f7f5f; ">// size of C array must be equal or greater than</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: #3f7f5f; ">// sum of A and B arrays' sizes</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">public</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">void</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;merge(</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">[] A,&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">[] B,&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">[] C) {</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>int i,j,k ;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; i = 0;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; j=0;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; k=0;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; m = A.length;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; n = B.length;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; while(i &lt; m &amp;&amp; j &lt; n){</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(A[i]&lt;= B[j]){</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C[k] = A[i];</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C[k] = B[j];</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp;}</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp;k++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp;while(i&lt;m){</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C[k] = A[i]</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; }</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; while(j&lt;n){</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C[k] = B[j]&nbsp;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k++;</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>&nbsp;}</strong></font></span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong><br />
            </strong></font></span></p>
            <h3 style="font-size: 12pt; ">Python &nbsp;implementation</h3>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">def merege(left,right):</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; result = []</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; i,j = 0</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp;while i&lt; len(left) and j &lt; len(right):</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; if left[i]&lt;= right[j]:</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(left[i])</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = i + 1</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; else:</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(right[j])</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = j + 1</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; while i&lt; len(left):</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result.append(left[i])</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i = i + 1</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; while　j&lt; len(right):</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result.append(right[j])</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j = j + 1</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp; &nbsp; return result</font></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font color="#008000" face="'Courier New'" size="2">&nbsp;&nbsp;<br />MergSort:<br /><br /></font><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">import</span>&nbsp;operator<br /><br /><span style="color: #0000FF; ">def</span>&nbsp;mergeSort(L,&nbsp;compare&nbsp;=&nbsp;operator.lt):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;len(L)&nbsp;&lt;&nbsp;2:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;L[:]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;middle&nbsp;=&nbsp;int(len(L)/2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;=&nbsp;mergeSort(L[:middle],&nbsp;compare)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right=&nbsp;mergeSort(L[middle:],&nbsp;compare)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;merge(left,&nbsp;right,&nbsp;compare)<br /><br /><span style="color: #0000FF; ">def</span>&nbsp;merge(left,&nbsp;right,&nbsp;compare):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;[]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i,&nbsp;j&nbsp;=&nbsp;0,&nbsp;0<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;i&nbsp;&lt;&nbsp;len(left)&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;j&nbsp;&lt;&nbsp;len(right):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;compare(left[i],&nbsp;right[j]):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.append(left[i])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;+=&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.append(right[j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;+=&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;i&nbsp;&lt;&nbsp;len(left):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.append(left[i])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;+=&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;j&nbsp;&lt;&nbsp;len(right):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.append(right[j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;+=&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;result<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><font color="#008000" face="'Courier New'" size="2"><br /></font></p>
            </div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table><img src ="http://www.blogjava.net/ytl-zlq/aggbug/349692.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ytl-zlq/" target="_blank">ytl</a> 2011-05-06 16:55 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349692.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sorting algorithms --Selection Sort</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349687.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Fri, 06 May 2011 08:16:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349687.html</guid><description><![CDATA[<table style="background-color: #f5f5f5; margin-left: auto; margin-right: auto; ">
    <tbody>
        <tr>
            <td>
            <div class="width" style="min-width: 960px; width: 960px; margin-left: auto; margin-right: auto;">
            <div class="padding">
            <div class="content" style="padding: 0px 25px 25px; margin-bottom: 2px; vertical-align: top; width: 739px; margin-right: -1px; border-left-color: #dddddd; background-color: #ffffff;">
            <div class="inside" style="margin-top: 16px;">
            <h1 style="font-size: 18pt; ">Selection Sort</h1>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Selection sort is one of the O(n<sup>2</sup>) sorting algorithms, which makes it quite inefficient for sorting large data volumes. Selection sort is notable for its programming simplicity and it can over perform other sorts in certain&nbsp;<span style="font-family: 'Times New Roman'; font-size: 12pt; ">situations</span>&nbsp;(see complexity analysis for more details).</p>
            <h2 style="font-size: 14pt; ">Algorithm</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">The idea of algorithm is quite simple. Array is imaginary divided into two parts -&nbsp;<span class="style1" style="color: #70b272; font-weight: bold; ">sorted one</span>&nbsp;and&nbsp;<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted one</span>. At the beginning,&nbsp;<span class="style1" style="color: #70b272; font-weight: bold; ">sorted part</span>&nbsp;is&nbsp;<strong>empty</strong>, while&nbsp;<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted one</span>&nbsp;contains&nbsp;<strong>whole array</strong>.&nbsp;<em>At every step,</em>&nbsp;algorithm finds&nbsp;<strong>minimal element</strong>&nbsp;in the&nbsp;<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted part</span>&nbsp;and adds it to the end of the<span class="style1" style="color: #70b272; font-weight: bold; ">&nbsp;sorted one</span>. When&nbsp;<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted part</span>&nbsp;becomes&nbsp;<strong>empty</strong>, algorithm<em>stops</em>.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">When algorithm sorts an array, it swaps first element of unsorted part with minimal element and then it is included to the sorted part. This implementation of selection sort in&nbsp;<strong>not stable</strong>. In case of linked list is sorted, and, instead of swaps, minimal element is linked to the unsorted part, selection sort is&nbsp;<strong>stable</strong>.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Let us see an example of sorting an array to make the idea of selection sort clearer.</p>
            <p class="style4" style="text-align: justify; letter-spacing: 0.2px; font-family: 'Times New Roman', Times, serif; "><em>Example.&nbsp;</em>Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/selection-sort-1.png" alt="Selection sort example" /></p>
            <h2 style="font-size: 14pt; ">Complexity analysis</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Selection sort stops, when unsorted part becomes empty. As we know, on every step number of unsorted elements decreased by one. Therefore, selection sort makes n steps (n is number of elements in array) of outer loop, before stop. Every step of outer loop requires finding minimum in unsorted part. Summing up, n + (n - 1) + (n - 2) + ... + 1, results in O(n<sup>2</sup>) number of comparisons. Number of swaps may vary from zero (in case of sorted array) to n - 1 (in case array was sorted in reversed order), which results in O(n) number of swaps. Overall algorithm complexity is O(n<sup>2</sup>).</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Fact, that selection sort requires n - 1 number of swaps at most, makes it very efficient in situations, when write operation is significantly more expensive, than read operation.</p>
            <h2 style="font-size: 14pt; ">Code snippets</h2>
            <h3 style="font-size: 12pt; ">Java</h3>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">public</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">void</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;selectionSort(</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">[] arr) {</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;i, j, minIndex, tmp;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;n = arr.length;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">for</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(i = 0; i &lt; n - 1; i++) {</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minIndex = i;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">for</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(j = i + 1; j &lt; n; j++)</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">if</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(arr[j] &lt; arr[minIndex])</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minIndex = j;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-family: 'Courier New'; font-size: 10pt; color: #7f0055; ">if</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;(minIndex != i) {</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmp = arr[i];</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr[i] = arr[minIndex];</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">arr[minIndex] = tmp;</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">}</span></p>
            <h3 style="font-size: 12pt; ">Python</h3>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman'; "><div>&nbsp; &nbsp; &nbsp;for i in range(len(L)-1):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minIndex = i</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minValue = L[i]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = i + 1</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while j&lt; len(L):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if minValue &gt; L[j]:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minIndex = j</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minValue = L[j]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j += 1</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if minIndex != i:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp &nbsp; &nbsp; &nbsp; = L[i]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L[i] &nbsp; &nbsp; &nbsp; = L[minIndex]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L[minIndex] = temp</div></p>
            <p class="MsoNormal" style="text-align: justify; letter-spacing: 0.2px; margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; "><font face="'Courier New'" size="2"><br />
            </font></p>
            </div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table><img src ="http://www.blogjava.net/ytl-zlq/aggbug/349687.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ytl-zlq/" target="_blank">ytl</a> 2011-05-06 16:16 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/05/06/349687.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>