﻿<?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-OneEyeWolf &amp; B2B &amp; B2C-随笔分类-Algorithm theory &amp; practice</title><link>http://www.blogjava.net/OneEyeWolf/category/13637.html</link><description>致力于电子商务平台框架的开发</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 04:10:19 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 04:10:19 GMT</pubDate><ttl>60</ttl><item><title>List集合随机排序算法分析</title><link>http://www.blogjava.net/OneEyeWolf/archive/2006/07/31/61066.html</link><dc:creator>OneEyeWolf</dc:creator><author>OneEyeWolf</author><pubDate>Mon, 31 Jul 2006 10:25:00 GMT</pubDate><guid>http://www.blogjava.net/OneEyeWolf/archive/2006/07/31/61066.html</guid><wfw:comment>http://www.blogjava.net/OneEyeWolf/comments/61066.html</wfw:comment><comments>http://www.blogjava.net/OneEyeWolf/archive/2006/07/31/61066.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/OneEyeWolf/comments/commentRss/61066.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/OneEyeWolf/services/trackbacks/61066.html</trackback:ping><description><![CDATA[
		<p>List集合随机排序算法分析</p>
		<p>先说一下JDK 对List的随机排序的实现：</p>
		<p>public static void shuffle(List list, Random rnd) {　　　　<br />    　　final int SHUFFLE_THRESHOLD        =    5;　　//这应当是一个经验值吧</p>
		<p>        int size = list.size();<br />　　/** 为什么要判断，后面有论述 */<br />        if (size &lt; SHUFFLE_THRESHOLD || list instanceof RandomAccess) {<br />            for (int i=size; i&gt;1; i--)<br />                swap(list, i-1, rnd.nextInt(i));  //这一种方法是直接调用List.set方法，属于RandomAccess中的方法<br />        } else {<br />            Object arr[] = list.toArray();</p>
		<p>            // Shuffle array<br />            for (int i=size; i&gt;1; i--)<br />                swap(arr, i-1, rnd.nextInt(i));</p>
		<p>            // Dump array back into list<br />            ListIterator it = list.listIterator();      //如果不是Random Access实现，就使用迭代器遍历<br />            for (int i=0; i&lt;arr.length; i++) {<br />                it.next();<br />                it.set(arr[i]);<br />            }<br />        }<br />    }</p>
		<p>
				<br />再说一下我自己的笨拙实现：<br />　　public static List randomSortList(List ls, Random random) {<br />                List randomList = new ArrayList();                <br />                int size = list.size();<br />               while (size &gt; 0) {<br />                  int randomNum = random.nextInt(size);<br />                  randomList.add(ls.get(randomNum));<br />                  ls.remove(randomNum); //这一步，对于RandomAccess的集合来说，是O(1)操作<br />               }<br />               return randomList;<br />     }<br />评述：<br />　　如果List的实现是ArrayList，在时间效率上要多循环一次，但在空间上，我的方法非常差，多生成一个List集合，如果List很大，就更差了。同时我的算法如果用于Sequence List上，效率是相当的差，只能适合ArrayList,有很大的局限性。<br />　　这是因为： 如果集合类是RandomAccess的实现，则尽量用for(int i = 0; i &lt; size; i++) 来遍历而不要用Iterator迭代器来遍历，在效率上要差一些。反过来，如果List是Sequence List，则最好用迭代器来进行迭代。<br />　　JDK中说的很清楚，在对List特别是Huge size的List的遍历算法中，要尽量来判断是属于RandomAccess(如ArrayList)还是Sequence List (如LinkedList），因为适合RandomAccess List的遍历算法，用在Sequence List上就差别很大，常用的作法就是：<br />    要作一个判断：<br />    if (list instance of RandomAccess) {<br />        for(int m = 0; m &lt; list.size(); m++){}<br />    }else{<br />        Iterator iter = list.iterator();<br />        while(iter.hasNext()){}<br />    }<br />   我的项目中List都是基于ArrayList的，所以基本上很少用迭代器来遍历，而是用for循环来遍历，对于迭代器的作用我当然很清楚，但是我觉得有点庸人自扰了。<br />   除非你经常用Collection作为你的接口方法中的输入或输出的集合参数类型时，你也就只能用Iterator。<br />   但我一般在接口方法中，一般用List，所以我就不用迭代器，除非我的List是Linked List实例。<br />   好的作法是：在供外部调用的接口方法中，使用Collection作为集合参数类型，在内部实现当中，使用List，而不是一味的使用Collections及Iterator，这样做无异于作茧自缚。<br />   顺便说一下JDK中推荐的是对List集合尽量要实现RandomAccess接口。<br /></p>
<img src ="http://www.blogjava.net/OneEyeWolf/aggbug/61066.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/OneEyeWolf/" target="_blank">OneEyeWolf</a> 2006-07-31 18:25 <a href="http://www.blogjava.net/OneEyeWolf/archive/2006/07/31/61066.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>