C# List打乱顺序

LIST排序是很慢的,特别是数量比较大的时候,网上大部分都是采用新建List后Insert的方式,这个是真的慢,而且在数据量大的时候很容易内存溢出。

采用下面这种值交换的方式效果要比上面那种强很多。

代码

List<int> tmpList = new List<int>(99999999);
for (int i = 0; i <99999999; i++)
{
    tmpList.Add(i);                  
}
Random rnd = new Random();
for (int i = 0; i < tmpList.Count; i++)
{
	int idx = rnd.Next(tmpList.Count-1);
	int val = tmpList[i]; 
	tmpList[i] = tmpList[idx];
	tmpList[idx] = val;                    
}


标签:
版权所有原创文章,转载请保留或注明出处:https://261k.com/post/20190805093051.html