日期:2014-05-20  浏览次数:21051 次

算法大比拼,类型数组如何实现乱序排列
我有一个
C# code
MyClass[] myClassList

如何打乱数组的顺序呢,随机的,求简洁或最优算法

ps:MyClass类中当然有个唯一标示属性。即GUID.

------解决方案--------------------
不知道有没有好的写法?
C# code
            //设置顺序号以方便观察
            MyClass[] myClassList = new MyClass[ 100 ];
            for( int i = 0 ; i < myClassList.Length-1 ; i++ ) {
                myClassList[ i ] = new MyClass( i );
            }
            
            //随机顺序
            Random rd = new Random( Environment.TickCount );
            for( int i = 0 ; i < myClassList.Length - 1 ; i++ ) {
                int x = rd.Next( 0, 99 );
                int y = rd.Next( 0, 99 );
                MyClass temp = myClassList[ x ];
                myClassList[ x ] = myClassList[ y ];
                myClassList[ y ] = temp;
            }

            MessageBox.Show( myClassList.Length.ToString() );

------解决方案--------------------
不知道有没有好的写法...代码补全了
C# code
        class MyClass {
            public Guid MyGuid { get; set; }
            public int ID { get; set; }
            public MyClass(int i) {
                this.MyGuid = Guid.NewGuid();
                this.ID = i;
            }
        }
        private void button1_Click( object sender, EventArgs e ) {
            //设置顺序号以方便观察
            MyClass[] myClassList = new MyClass[ 100 ];
            for( int i = 0 ; i < myClassList.Length-1 ; i++ ) {
                myClassList[ i ] = new MyClass( i );
            }
            
            //随机顺序
            Random rd = new Random( Environment.TickCount );
            for( int i = 0 ; i < myClassList.Length - 1 ; i++ ) {
                int x = rd.Next( 0, 99 );
                int y = rd.Next( 0, 99 );
                MyClass temp = myClassList[ x ];
                myClassList[ x ] = myClassList[ y ];
                myClassList[ y ] = temp;
            }

------解决方案--------------------
List<MyClass> srcList = new List<MyClass>(myClassList);
List<MyClass> destList = new List<MyClass>(myClassList.Length);
System.Random rnd = new Random();
while (srcList.Count > 0)
{
int tmpIndex = rnd.Next(0, srcList.Count);
destList.Add(srcList[tmpIndex]);
srcList.RemoveAt(tmpIndex);
}
MyClass[] destMyClassList = destList.ToArray();
------解决方案--------------------
C# code
MyClass[] myClassList = ...;
byte[] keys = new byte[myClassList.Length];
(new Random()).NextBytes(keys);
Array.Sort(keys, myClassList);

------解决方案--------------------
//谢狼兄,又学到新内容了,陌生的概念啊,备注下
http://jonny131.javaeye.com/blog/188829
C# code
        
        /*
         * 线性同余法求伪随机数的公式:
          * X(n+1) = (a Xn + c) mod m
         */
        uint Rand( uint seed ) {
            return (uint)( seed * 1103515245 + 12345 ) % 32768;
        }

------解决方案--------------------
探讨
引用:
C# codeMyClass[] myClassList= ...;byte[] keys=newbyte[myClassList.Length];
(new Random()).NextBytes(keys);
Array.Sort(keys, myClassList);


进行Sort没有必要!选择两个[0...myClassList.Length)之间的随机数,把它们做下标的两个元素交换位置。这样重复超过 myClassList.Length/2 次就可以了。

------解决方案--------------------
偶用的洗牌算法,感觉效果还不错
int temp, index0;
for (int i = 1; i < len; i++) //洗牌算法
{
index0 = ra.Next(i);
temp = Sequence[index0];