﻿<?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;</title><link>http://www.blogjava.net/ytl-zlq/</link><description>&lt;font size="4" &gt;厚积而薄发---每一天都是一个全新的开始&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Sun, 03 May 2026 07:09:34 GMT</lastBuildDate><pubDate>Sun, 03 May 2026 07:09:34 GMT</pubDate><ttl>60</ttl><item><title>最大公约数</title><link>http://www.blogjava.net/ytl-zlq/archive/2013/03/21/396781.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Thu, 21 Mar 2013 01:39:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2013/03/21/396781.html</guid><wfw:comment>http://www.blogjava.net/ytl-zlq/comments/396781.html</wfw:comment><comments>http://www.blogjava.net/ytl-zlq/archive/2013/03/21/396781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ytl-zlq/comments/commentRss/396781.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ytl-zlq/services/trackbacks/396781.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 欧几里德算法&nbsp;&nbsp;<a href='http://www.blogjava.net/ytl-zlq/archive/2013/03/21/396781.html'>阅读全文</a><img src ="http://www.blogjava.net/ytl-zlq/aggbug/396781.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> 2013-03-21 09:39 <a href="http://www.blogjava.net/ytl-zlq/archive/2013/03/21/396781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jvm的内存模型之eden区   转载</title><link>http://www.blogjava.net/ytl-zlq/archive/2012/03/01/371093.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Thu, 01 Mar 2012 10:12:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2012/03/01/371093.html</guid><wfw:comment>http://www.blogjava.net/ytl-zlq/comments/371093.html</wfw:comment><comments>http://www.blogjava.net/ytl-zlq/archive/2012/03/01/371093.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ytl-zlq/comments/commentRss/371093.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ytl-zlq/services/trackbacks/371093.html</trackback:ping><description><![CDATA[<div style="color: #4e4e4e; line-height: normal; text-align: left; "><h3><font face="微软雅黑, 黑体, Arial, Helvetica, sans-serif"><span style="font-size: 20px;"><br /></span></font></h3><p clearfix="" nbw-act=""  fc06"="" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; font-size: 12px; zoom: 1; margin-top: 0px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #666666; line-height: 20px; "><strong style="color: #4e4e4e; line-height: 25px; ">浅谈java内存模型&nbsp;</strong></p></div><div fc05="" fc11="" nbw-blog="" ztag=""  js-fs2"="" style="line-height: 25px; text-align: left; word-wrap: break-word; color: #4e4e4e; margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; overflow-y: hidden; font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不同的平台，内存模型是不一样的，但是jvm的内存模型规范是统一的。其实java的多线程并发问题最终都会反映在java的内存模型上，所谓线程安全无 非是要控制多个线程对某个资源的有序访问或修改。总结java的内存模型，要解决两个主要的问题：可见性和有序性。我们都知道计算机有高速缓存的存在，处 理器并不是每次处理数据都是取内存的。JVM定义了自己的内存模型，屏蔽了底层平台内存管理细节，对于java开发人员，要清楚在jvm内存模型的基础 上，如果解决多线程的可见性和有序性。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 那么，何谓<strong>可见性</strong>？ 多个线程之间是不能互相传递数据通信的，它们之间的沟通只能通过共享变量来进行。Java内存模型（JMM）规定了jvm有主内存，主内存是多个线程共享 的。当new一个对象的时候，也是被分配在主内存中，每个线程都有自己的工作内存，工作内存存储了主存的某些对象的副本，当然线程的工作内存大小是有限制 的。当线程操作某个对象时，执行顺序如下：<br />&nbsp;(1) 从主存复制变量到当前工作内存 (read and load)<br />&nbsp;(2) 执行代码，改变共享变量值 (use and assign)<br />&nbsp;(3) 用工作内存数据刷新主存相关内容 (store and write)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">JVM规范定义了线程对主存的操作指 令：read，load，use，assign，store，write。当一个共享变量在多个线程的工作内存中都有副本时，如果一个线程修改了这个共享 变量，那么其他线程应该能够看到这个被修改后的值，这就是多线程的可见性问题。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 那么，什么是<strong>有序性</strong>呢 ？线程在引用变量时不能直接从主内存中引用,如果线程工作内存中没有该变量,则会从主内存中拷贝一个副本到工作内存中,这个过程为read-load,完 成后线程会引用该副本。当同一线程再度引用该字段时,有可能重新从主存中获取变量副本(read-load-use),也有可能直接引用原来的副本 (use),也就是说 read,load,use顺序可以由JVM实现系统决定。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 线程不能直接为主存中中字段赋值，它会将值指定给工作内存中的变量副本(assign),完成后这个变量副本会同步到主存储区(store- write)，至于何时同步过去，根据JVM实现系统决定.有该字段,则会从主内存中将该字段赋值到工作内存中,这个过程为read-load,完成后线 程会引用该变量副本，当同一线程多次重复对字段赋值时,比如：<br />Java代码&nbsp;<br />for(int i=0;i&lt;10;i++)&nbsp;&nbsp;&nbsp;<br />&nbsp;a++;&nbsp;&nbsp;<br />线程有可能只对工作内存中的副本进行赋值,只到最后一次赋值后才同步到主存储区，所以assign,store,weite顺序可以由JVM实现系统决 定。假设有一个共享变量x，线程a执行x=x+1。从上面的描述中可以知道x=x+1并不是一个原子操作，它的执行过程如下：<br />1 从主存中读取变量x副本到工作内存<br />2 给x加1<br />3 将x加1后的值写回主 存<br />如果另外一个线程b执行x=x-1，执行过程如下：<br />1 从主存中读取变量x副本到工作内存<br />2 给x减1<br />3 将x减1后的值写回主存&nbsp;<br />那么显然，最终的x的值是不可靠的。假设x现在为10，线程a加1，线程b减1，从表面上看，似乎最终x还是为10，但是多线程情况下会有这种情况发生：<br />1：线程a从主存读取x副本到工作内存，工作内存中x值为10<br />2：线程b从主存读取x副本到工作内存，工作内存中x值为10<br />3：线程a将工作内存中x加1，工作内存中x值为11<br />4：线程a将x提交主存中，主存中x为11<br />5：线程b将工作内存中x值减1，工作内存中x值为9<br />6：线程b将x提交到中主存中，主存中x为</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><strong>jvm的内存模型之eden区</strong><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">所谓线程的&#8220;工作内存&#8221;到底是个什么东西？有的人认为是线程的栈，其实这种理解是不正确的。看看JLS（java语言规范）对线程工作 内存的描述，线程的working memory只是cpu的<strong>寄存器和高速缓存的抽象描述</strong>。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可能 很多人都觉得莫名其妙，说JVM的内存模型，怎么会扯到cpu上去呢？在此，我认为很有必要阐述下，免 得很多人看得不明不白的。先抛开java虚拟机不谈，我们都知道，现在的计算机，cpu在计算的时候，并不总是从内存读取数据，它的数据读取顺序优先级 是：寄存器－高速缓存－内存。线程耗费的是CPU，线程计算的时候，原始的数据来自内存，在计算过程中，有些数据可能被频繁读取，这些数据被存储在寄存器 和高速缓存中，当线程计算完后，这些缓存的数据在适当的时候应该写回内存。当个多个线程同时读写某个内存数据时，就会产生多线程并发问题，涉及到三个特 性：原子性，有序性，可见性。在《线程安全总结》这篇文章中，为了理解方便，我把原子性和有序性统一叫做&#8220;多线程执行有序性&#8221;。支持多线程的平台都会面临 这种问题，运行在多线程平台上支持多线程的语言应该提供解决该问题的方案。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; synchronized, volatile,锁机制（如同步块，就绪队 列，阻塞队列）</strong>等等。这些方案只是语法层面的，但我们要从本质上去理解它，不能仅仅知道一个 synchronized 可以保证同步就完了。&nbsp;&nbsp; 在这里我说的是jvm的内存模型，是动态的，面向多线程并发的，沿袭JSL的&#8220;working memory&#8221;的说法，只是不想牵扯到太多底层细节，因为 《线程安全总结》这篇文章意在说明怎样从语法层面去理解java的线程同步，知道各个关键字的使用场 景。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">说说JVM的eden区吧。JVM的内存，被划分了很多的区域：</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1.程序计数器<br />每一个Java线程都有一个程序计数器来用于保存程序执行到当前方法的哪一个指令。<br />2.线程栈<br />线程的每个方法被执行的时候，都会同时创建一个帧（Frame）用于存储本地变量表、操作栈、动态链接、方法出入口等信息。每一个方法的调用至完成，就意味着一个帧在VM栈中的入栈至出栈的过程。如果线程请求的栈深度大于虚拟机所允许的深度，将抛出StackOverflowError异常；如果VM栈可以动态扩展（VM Spec中允许固定长度的VM栈），当扩展时无法申请到足够内存则抛出OutOfMemoryError异常。<br />3.本地方法栈<br />4.堆<br />每个线程的栈都是该线程私有的，堆则是所有线程共享的。当我们new一个对象时，该对象就被分配到了堆中。但是堆，并不是一个简单的概念，堆区又划分了很多区域，为什么堆划分成这么多区域，这是为了JVM的内存垃圾收集，似乎越扯越远了，扯到垃圾收集了，现在的jvm的gc都是按代收集，堆区大致被分为三大块：新生代，旧生代，持久代（虚拟的）；新生代又分为eden区，s0区，s1区。新建一个对象时，基本小的对象，生命周期短的对象都会放在新生代的eden区中，eden区满时，有一个小范围的gc（minor gc），整个新生代满时，会有一个大范围的gc（major gc），将新生代里的部分对象转到旧生代里。<br />5.方法区&nbsp;<br />其实就是永久代（Permanent Generation），方法区中存放了每个Class的结构信息，包括常量池、字段描述、方法描述等等。VM Space描述中对这个区域的限制非常宽松，除了和Java堆一样不需要连续的内存，也可以选择固定大小或者可扩展外，甚至可以选择不实现垃圾收集。相对来说，垃圾收集行为在这个区域是相对比较少发生的，但并不是某些描述那样永久代不会发生GC（至 少对当前主流的商业JVM实现来说是如此），这里的GC主要是对常量池的回收和对类的卸载，虽然回收的&#8220;成绩&#8221;一般也比较差强人意，尤其是类卸载，条件相当苛刻。<br />6.常量池<br />&nbsp;Class文件中除了有类的版本、字段、方法、接口等描述等信息外，还有一项信息是常量表(constant_pool table)，用于存放编译期已可知的常量，这部分内容将在类加载后进入方法区（永久代）存放。但是Java语言并不要求常量一定只有编译期预置入Class的常量表的内容才能进入方法区常量池，运行期间也可将新内容放入常量池（最典型的String.intern()方法）。</p></div><img src ="http://www.blogjava.net/ytl-zlq/aggbug/371093.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> 2012-03-01 18:12 <a href="http://www.blogjava.net/ytl-zlq/archive/2012/03/01/371093.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 原码代码学习</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/09/24/359414.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Sat, 24 Sep 2011 07:30:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/09/24/359414.html</guid><description><![CDATA[ArrayList<br />&nbsp; &nbsp; &nbsp; &nbsp;关于<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #efefef; ">Java中的transient，volatile和strictfp关键字&nbsp;</span>http://www.iteye.com/topic/52957<br />&nbsp; &nbsp; &nbsp; &nbsp;(1), ArrayList底层使用Object数据实现，&nbsp;private transient Object[] elementData;且在使用不带参数的方式实例化时，生成数组默认的长度是10。<br />&nbsp; &nbsp; &nbsp; (2), &nbsp;add方法实现<br /><div><div>&nbsp; &nbsp; &nbsp; public boolean add(E e) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//ensureCapacityInternal判断添加新元素是否需要重新扩大数组的长度，需要则扩否则不</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ensureCapacityInternal(size + 1); &nbsp;// 此为JDK7调用的方法 JDK5里面使用的ensureCapacity方法</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementData[size++] = e; //把对象插入数组，同时把数组存储的数据长度size加1</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;</div><div>&nbsp; &nbsp; &nbsp; }</div></div><div>&nbsp; &nbsp; &nbsp;JDK 7中&nbsp;ensureCapacityInternal实现<br /><div><div>&nbsp; &nbsp;private void ensureCapacityInternal(int minCapacity) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; modCount++;修改次数</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // overflow-conscious code</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (minCapacity - elementData.length &gt; 0)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grow(minCapacity);//如果需要扩大数组长度</div><div>&nbsp; &nbsp; }<br /><div><div>/**</div><div>&nbsp; &nbsp; &nbsp;* The maximum size of array to allocate. --申请新数组最大长度</div><div>&nbsp; &nbsp; &nbsp;* Some VMs reserve some header words in an array.</div><div>&nbsp; &nbsp; &nbsp;* Attempts to allocate larger arrays may result in</div><div>&nbsp; &nbsp; &nbsp;* OutOfMemoryError: Requested array size exceeds VM limit &nbsp;--如果申请的数组占用的内心大于JVM的限制抛出异常</div><div>&nbsp; &nbsp; &nbsp;*/</div><div>&nbsp; &nbsp; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;//为什么减去8看注释第2行</div><div></div><div>&nbsp; &nbsp; /**</div><div>&nbsp; &nbsp; &nbsp;* Increases the capacity to ensure that it can hold at least the</div><div>&nbsp; &nbsp; &nbsp;* number of elements specified by the minimum capacity argument.</div><div>&nbsp; &nbsp; &nbsp;*</div><div>&nbsp; &nbsp; &nbsp;* @param minCapacity the desired minimum capacity</div><div>&nbsp; &nbsp; &nbsp;*/</div><div>&nbsp; &nbsp; private void grow(int minCapacity) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // overflow-conscious code</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int oldCapacity = elementData.length;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1); //新申请的长度为old的3/2倍同时使用位移运算更高效，JDK5中：&nbsp;(oldCapacity *3)/2+1</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (newCapacity - minCapacity &lt; 0) &nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCapacity = minCapacity;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (newCapacity - MAX_ARRAY_SIZE &gt; 0) //你懂的</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCapacity = hugeCapacity(minCapacity);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // minCapacity is usually close to size, so this is a win:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; elementData = Arrays.copyOf(elementData, newCapacity);</div><div>&nbsp; &nbsp; }</div><div>&nbsp;//可以申请的最大长度</div><div>&nbsp; &nbsp; private static int hugeCapacity(int minCapacity) {&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (minCapacity &lt; 0) // overflow</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new OutOfMemoryError();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return (minCapacity &gt; MAX_ARRAY_SIZE) ?</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer.MAX_VALUE :</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MAX_ARRAY_SIZE;</div><div>&nbsp; &nbsp; }</div></div></div></div></div><br /><br /><a href="http://www.iteye.com/topic/52957"><br /></a><img src ="http://www.blogjava.net/ytl-zlq/aggbug/359414.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-09-24 15:30 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/09/24/359414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>计量点、计量分类等</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/08/07/355934.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Sun, 07 Aug 2011 03:02:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/08/07/355934.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.blogjava.net/ytl-zlq/archive/2011/08/07/355934.html'>阅读全文</a><img src ="http://www.blogjava.net/ytl-zlq/aggbug/355934.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-08-07 11:02 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/08/07/355934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Alogrithms to quicksort</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/05/08/349777.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Sun, 08 May 2011 06:13:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/05/08/349777.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; ">Quicksort</h1>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Quicksort is a fast sorting algorithm, which is used not only for educational purposes, but widely applied in practice. On the average, it has O(n log n) complexity, making quicksort suitable for sorting big data volumes. The idea of the algorithm is quite simple and once you realize it, you can write quicksort as fast as&nbsp;<a href="http://www.algolist.net/Algorithms/Sorting/Bubble_sort" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">bubble sort</a>.</p>
            <h2 style="font-size: 14pt; ">Algorithm</h2>
            The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:
            <ol>
                <li><strong>Choose a pivot value.&nbsp;</strong>We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array.</li>
                <li><strong>Partition.&nbsp;</strong>Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.</li>
                <li><strong>Sort both parts.&nbsp;</strong>Apply quicksort algorithm recursively to the left and the right parts.</li>
            </ol>
            <h3 style="font-size: 12pt; ">Partition algorithm in detail</h3>
            <p style="text-align: justify; letter-spacing: 0.2px; ">There are two indices&nbsp;<strong>i</strong>&nbsp;and&nbsp;<strong>j</strong>&nbsp;and at the very beginning of the&nbsp;<span style="font-family: 'Times New Roman'; font-size: 12pt; ">partition&nbsp;</span>algorithm&nbsp;<strong>i</strong>&nbsp;points to the first element in the array and<strong>j</strong>&nbsp;points to the last one. Then algorithm moves&nbsp;<strong>i</strong>&nbsp;forward, until an element with value greater or equal to the pivot is found. Index&nbsp;<strong>j</strong>&nbsp;is moved backward, until an element with value lesser or equal to the pivot is found. If&nbsp;<strong>i &#8804; j&nbsp;</strong>then they are swapped and i steps to the next position (<strong>i + 1</strong>), j steps to the previous one&nbsp;<strong>(j - 1)</strong>. Algorithm stops, when<strong>&nbsp;i</strong>&nbsp;becomes greater than&nbsp;<strong>j</strong>.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">After partition, all values before&nbsp;<strong>i-th</strong>&nbsp;element are less or equal than the pivot and all values after&nbsp;<strong>j-th</strong>&nbsp;element are greater or equal to the pivot.</p>
            <em>Example.&nbsp;</em>Sort {1, 12, 5, 26, 7, 14, 3, 7, 2} using quicksort.
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/quick-sort.png" alt="Quicksort example" /></p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Notice, that we show here only the first recursion step, in order not to make example too long. But, in fact, {1, 2, 5, 7, 3} and {14, 7, 26, 12} are sorted then recursively.</p>
            <h2 style="font-size: 14pt; ">Why does it work?</h2>
            On the partition step algorithm divides the array into two parts and every element&nbsp;<strong>a</strong>&nbsp;from the left part is less or equal than every element&nbsp;<strong>b</strong>&nbsp;from the right part. Also&nbsp;<strong>a</strong>&nbsp;and&nbsp;<strong>b</strong>&nbsp;satisfy&nbsp;<strong>a &#8804; pivot &#8804; b</strong>&nbsp;inequality. After completion of the recursion calls both of the parts become sorted and, taking into account arguments stated above,<span style="color: #808080; ">&nbsp;</span>the whole array is sorted.<br />
            <h2 style="font-size: 14pt; ">Complexity analysis</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">On the average quicksort has O(n log n) complexity, but strong proof of this fact is not trivial and not presented here. Still, you can find the proof in&nbsp;<a href="http://www.algolist.net/Algorithms/Sorting/Quicksort#cormen_book" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; text-decoration: none; ">[1]</a>. In worst case, quicksort runs O(n<sup>2</sup>) time, but on the most "practical" data it works just fine and outperforms other O(n log n) sorting algorithms.</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; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;partition(</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;arr[],&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; ">{</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;int i = left;</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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp;int j = 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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp;int temp;</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 face="'Courier New'" size="2">&nbsp; &nbsp; int &nbsp;pivot = arr[(left+right)&gt;&gt;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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp;while(i&lt;=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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; while(arr[i]&gt;=pivot){</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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; }</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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; while(arr[j]&lt;=pivot){</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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp;}</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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp;if(i&lt;=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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = arr[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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[i] = arr[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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[j] = temp;</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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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 face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp;}</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 face="'Courier New'" size="2">&nbsp; &nbsp; }</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 face="'Courier New'" size="2">&nbsp; &nbsp; return 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-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">}</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;</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; ">void</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;quickSort(</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;arr[],&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; ">int</span></strong><span style="font-family: 'Courier New'; font-size: 10pt; color: black; ">&nbsp;index = partition(arr, left, 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; if(left&lt;index-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;quickSort(arr,left,index-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; }</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; if(index&lt;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;quickSort(arr,index,right);&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; }</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; "><font color="#0000ff">def</font>&nbsp;<span style="color: #010001; ">quickSort(L</span>,<span style="color: #010001; ">left</span>,<span style="color: #010001; ">right</span>) {</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;<span style="color: #010001; ">i</span>&nbsp;=&nbsp;</span><span style="font-family: 'Courier New'; font-size: 10pt; "><span style="color: #010001; ">left</span></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; "><span style="color: #010001; ">&nbsp; &nbsp; &nbsp; j = right</span></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; "><span style="color: #010001; ">&nbsp; &nbsp; &nbsp; if right-left &lt;=1:</span></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 color="#010001" face="'Courier New'" size="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return L</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-size: 12pt; font-family: 'Times New Roman'; "><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">pivot</span>&nbsp;=&nbsp;<font color="#010001">L</font>[(<span style="color: #010001; ">left</span>&nbsp;+&nbsp;<span style="color: #010001; ">right</span>) &gt;&gt;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;&nbsp;&nbsp;&nbsp;<span style="color: green; ">/* partition */</span></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;<span style="color: blue; ">while</span>&nbsp;(<span style="color: #010001; ">i</span>&nbsp;&lt;=&nbsp;<span style="color: #010001; ">j</span>) {</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">while</span>&nbsp;(<font color="#010001">L</font>[<span style="color: #010001; ">i</span>] &lt;&nbsp;<span style="color: #010001; ">pivot</span>)</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">i</span>++;</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">while</span>&nbsp;(<font color="#010001">L</font>[<span style="color: #010001; ">j</span>] &gt;&nbsp;<span style="color: #010001; ">pivot</span>)</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">j</span>--;</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">if</span>&nbsp;(<span style="color: #010001; ">i</span>&nbsp;&lt;=&nbsp;<span style="color: #010001; ">j</span>) {</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#010001">L[i],L[j] = L[j],L[i]</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">i</span>++;</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">j</span>--;</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;&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; ">&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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green; ">/* recursion */</span></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;<span style="color: blue; ">if</span>&nbsp;(<span style="color: #010001; ">left</span>&nbsp;&lt;&nbsp;<span style="color: #010001; ">j</span>)</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">quickSort</span>(<span style="color: #010001; ">L</span>,&nbsp;<span style="color: #010001; ">left</span>,&nbsp;<span style="color: #010001; ">j</span>);</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;<span style="color: blue; ">if</span>&nbsp;(<span style="color: #010001; ">i</span>&nbsp;&lt;&nbsp;<span style="color: #010001; ">right</span>)</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; font-size: 10pt; color: #010001; ">quickSort</span><span style="font-family: 'Courier New'; font-size: 10pt; ">(<span style="color: #010001; ">L</span>,&nbsp;<span style="color: #010001; ">i</span>,&nbsp;<span style="color: #010001; ">right</span>);</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; ">}</span></p>
            </div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table><img src ="http://www.blogjava.net/ytl-zlq/aggbug/349777.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-08 14:13 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/05/08/349777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> Algorithms to Insertion Sort</title><link>http://www.blogjava.net/ytl-zlq/archive/2011/05/08/349773.html</link><dc:creator>ytl</dc:creator><author>ytl</author><pubDate>Sun, 08 May 2011 04:24:00 GMT</pubDate><guid>http://www.blogjava.net/ytl-zlq/archive/2011/05/08/349773.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; ">Insertion Sort</h1>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Insertion sort belongs to the O(n<sup>2</sup>) sorting algorithms. Unlike many sorting algorithms with quadratic complexity, it is actually applied in practice for sorting small arrays of data. For instance, it is used to improve&nbsp;<a href="http://www.algolist.net/Algorithms/Sorting/Quicksort" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">quicksort routine</a>. Some sources notice, that people use same algorithm ordering items, for example, hand of cards.</p>
            <h2 style="font-size: 14pt; ">Algorithm</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Insertion sort algorithm somewhat resembles&nbsp;<a href="http://www.algolist.net/Algorithms/Sorting/Selection_sort" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">selection sort</a>. Array is imaginary divided into two parts -&nbsp;<span class="style1" style="color: #70b272; font-weight: bold; ">sorted one</span>&nbsp;and<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;contains&nbsp;<strong>first element</strong>&nbsp;of the array and&nbsp;<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted one</span>&nbsp;contains the rest. At every step, algorithm takes&nbsp;<strong>first element</strong>&nbsp;in the&nbsp;<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted part</span>&nbsp;and&nbsp;<strong>inserts</strong>&nbsp;it to the right place of the<span class="style1" style="color: #70b272; font-weight: bold; ">&nbsp;sorted one.</span>&nbsp;When<span class="style2" style="color: #d15468; font-weight: bold; ">unsorted part</span>&nbsp;becomes&nbsp;<strong>empty</strong>, algorithm&nbsp;<em>stops</em>. Sketchy, insertion sort algorithm step looks like this:</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/insertion-sort-sketchy-before.png" alt="Insertion sort sketchy, before insertion" /></p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><strong>becomes</strong></p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/insertion-sort-sketchy-after.png" alt="Insertion sort sketchy, after insertion" /></p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">The idea of the sketch was originaly posted&nbsp;<a href="http://en.wikipedia.org/wiki/Insertion_sort" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">here</a>.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Let us see an example of insertion sort routine to make the idea of algorithm clearer.</p>
            <p class="style4" style="text-align: justify; letter-spacing: 0.2px; "><em>Example.&nbsp;</em>Sort {7, -5, 2, 16, 4} using insertion sort.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/insertion-sort-1.png" alt="Insertion sort example" /></p>
            <h2 style="font-size: 14pt; ">The ideas of insertion</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">The main operation of the algorithm is&nbsp;<strong>insertion</strong>. The task is to insert a value into the sorted part of the array. Let us see the variants of how we can do it.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><strong>"Sifting down" using swaps</strong></p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">The simplest way to insert next element into the sorted part is to sift it down, until it occupies correct position.&nbsp;<span style="font-family: 'Times New Roman'; font-size: 12pt; ">Initially&nbsp;</span>the element stays right after the sorted part. At each step algorithm compares the element with one before it and, if they stay in reversed order, swap them. Let us see an illustration.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/insertion-sort-sift-down.png" alt="insertion sort, sift down illustration" /></p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">This approach writes sifted element to temporary position many times. Next implementation eliminates those unnecessary writes.</p>
            <h3 style="font-size: 12pt; ">Shifting instead of swapping</h3>
            <p style="text-align: justify; letter-spacing: 0.2px; ">We can modify previous algorithm, so it will write sifted element only to the final correct position. Let us see an illustration.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; "><img src="http://www.algolist.net/img/sorts/insertion-sort-shifting.png" alt="insertion sort, shifting illustration" /></p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">It is the most commonly used modification of the insertion sort.</p>
            <h3 style="font-size: 12pt; ">Using binary search</h3>
            <p style="text-align: justify; letter-spacing: 0.2px; "><span style="color: #000000; ">It is reasonable</span>&nbsp;to use&nbsp;<a href="http://www.algolist.net/Algorithms/Binary_search" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">binary search algorithm</a>&nbsp;to find a proper place for insertion. This variant of the insertion sort is called<strong>binary insertion sort</strong>. After position for insertion is found, algorithm shifts the part of the array and inserts the element. This version has lower number of comparisons, but overall average complexity remains O(n<sup>2</sup>). From a practical point of view this improvement is not very important, because insertion sort is used on quite small data sets.</p>
            <h2 style="font-size: 14pt; ">Complexity analysis</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">Insertion sort's overall complexity is O(n<sup>2</sup>) on average, regardless of the method of insertion. On the almost sorted arrays insertion sort shows better performance, up to O(n) in case of applying insertion sort to a sorted array. Number of writes is O(n<sup>2</sup>) on average, but number of comparisons may vary depending on the insertion algorithm. It is O(n<sup>2</sup>) when shifting or swapping methods are used and O(n log n) for binary insertion sort.</p>
            <p style="text-align: justify; letter-spacing: 0.2px; ">From the point of view of practical application, an average complexity of the insertion sort is not so important. As it was mentioned above, insertion sort is applied to quite small data sets (from 8 to 12 elements). Therefore, first of all, a "practical performance" should be considered. In practice insertion sort outperforms most of the quadratic sorting algorithms, like&nbsp;<a href="http://www.algolist.net/Algorithms/Sorting/Selection_sort" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">selection sort</a>&nbsp;or&nbsp;<a href="http://www.algolist.net/Algorithms/Sorting/Bubble_sort" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; color: #006cab; ">bubble sort</a>.</p>
            <h2 style="font-size: 14pt; ">Insertion sort properties</h2>
            <ul>
                <li>adaptive (performance adapts to the initial order of elements);</li>
                <li>stable (insertion sort retains relative order of the same elements);</li>
                <li>in-place (requires constant amount of additional space);</li>
                <li>online (new elements can be added during the sort).</li>
            </ul>
            <h2 style="font-size: 14pt; ">Code snippets</h2>
            <p style="text-align: justify; letter-spacing: 0.2px; ">We show the idea of insertion with shifts in Java implementation and the idea of insertion using python code snippet.</p>
            <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'; "><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;insertionSort(</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><span style="font-family: 'Courier New'; font-size: 10pt; "><font color="#7f0055"><strong>int i,j,newValue;</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; for(i=1;i&lt;arr.length;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;newValue = arr[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;j=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;while(j&gt;0&amp;&amp;arr[j-1]&gt;newValue){</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; &nbsp;arr[j] = arr[j-1];</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; &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; &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; &nbsp;arr[j] = newValue;</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>}</strong></font></span></p>
            <h3 style="font-size: 12pt; ">Python 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: blue; ">void</span><span style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp;<span style="color: #010001; ">insertionSort</span>(<font color="#0000ff">L</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;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">for</span>&nbsp;i in range(l,len(L)):</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">j</span>&nbsp;=&nbsp;<span style="color: #010001; ">i</span></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; newValue = L[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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">while</span>&nbsp;<span style="color: #010001; ">j</span>&nbsp;&gt; 0 and&nbsp;&nbsp;<font color="#010001">L</font>[<span style="color: #010001; ">j</span>&nbsp;- 1] &gt;<font color="#010001">L</font>[<span style="color: #010001; ">j</span>]:</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; &nbsp; &nbsp; &nbsp;L[<span style="color: #010001; ">j</span>] =&nbsp;<span style="color: #010001; ">L</span>[<span style="color: #010001; ">j</span>&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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">j = j-1</span></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;&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; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L[j] = newValue</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; }</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; ">}</span></p>
            </div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/ytl-zlq/aggbug/349773.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-08 12:24 <a href="http://www.blogjava.net/ytl-zlq/archive/2011/05/08/349773.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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>