算法大比拼,类型数组如何实现乱序排列

FlowerJunjie 2009-07-15 02:01:10
加精
我有一个
MyClass[] myClassList

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

ps:MyClass类中当然有个唯一标示属性。即GUID.
...全文
2135 75 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
75 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingjing_ge 2010-07-25
  • 打赏
  • 举报
回复
路过,学习哈……
专爆菜鸟 2010-07-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sp1234 的回复:]
引用 8 楼 vwxyzh 的回复:
C# codeMyClass[] myClassList= ...;byte[] keys=newbyte[myClassList.Length];
(new Random()).NextBytes(keys);
Array.Sort(keys, myClassList);


进行Sort没有必要!选择两个[0...myClassList.Len……
[/Quote]
支持
zhang3310028 2010-04-28
  • 打赏
  • 举报
回复
我的perl实现:
sub randomArray() {
my $ra_array = shift;
for ( my $i = 0 ; $i < @$ra_array ; $i++ ) {
my $index = int( rand(@$ra_array) );
@$ra_array[ $i, $index ] = @$ra_array[ $index, $i ];
}
}
ggggjatihc 2009-07-24
  • 打赏
  • 举报
回复

AssignListArray(array, LENGTH);
for (int i=LENGTH; i>=0; i--)
{
int index = rand() % i;
int tmp = array[i];
array[i] = array[index];
array[index] = tmp;
}

skynfly 2009-07-23
  • 打赏
  • 举报
回复
UP
清晨的老巢 2009-07-23
  • 打赏
  • 举报
回复
RANDOMIZE-IN-PLACE(A)
1 n ← length[A]
2 for i ← to n
3 do swap A[i] ↔ A[RANDOM(i, n)]

taotaojava 2009-07-20
  • 打赏
  • 举报
回复
学习一下
haorenhl007 2009-07-20
  • 打赏
  • 举报
回复
T MyArray = new T{...};
int i = 0;
int index;
Random random = new Random();
T temp;

while (i < MyArray.Length)
{
index = random.Next(MyArray.Length - i);
temp = MyArray[index];
MyArray[index] = MyArray[MyArray.Length - i];
MyArray[MyArray.Length - i] = temp;
i ++;
}
wocaonimadecoubi 2009-07-20
  • 打赏
  • 举报
回复
我只是拾人牙慧而已。
acdseeqq 2009-07-20
  • 打赏
  • 举报
回复
我的理解线性同余算法就是产生伪随机数,相当于自己做randowm,哪位大虾给我说说怎么用线性同余来优化这道题?
acdseeqq 2009-07-20
  • 打赏
  • 举报
回复
ArrayList^ myList=gcnew ArrayList;
//乱序
int i ,j;
Random^ myRandom=gcnew Random;
Object^ tem;
for(i=0;i< myList ->Count ;i++)
{
j= myRandom->Next (i,myList->Count );
tem= myList[i];
myList[i]=myList[j];
myList[j]=tem;
}
花落无痕IT 2009-07-20
  • 打赏
  • 举报
回复
有没有java 的写法呢!?
花落无痕IT 2009-07-20
  • 打赏
  • 举报
回复
有没有java 的写法呢!?
lfq08063440 2009-07-19
  • 打赏
  • 举报
回复
用到排序的算法都不是高效的基于比较的排序算法的时间下界是O(n×log(n));
用随机交换的的算法可以在线型时间完成。
benzite 2009-07-19
  • 打赏
  • 举报
回复
白用C#写了这么长时间程序了,看此贴简直就是雾里看花,只有学习的份,顶,以后好好学算法。
玖伍叁柒 2009-07-19
  • 打赏
  • 举报
回复
[Quote=引用 41 楼 superdullwolf 的回复:]
不知道线性同余法则的同学,拿刀自己叉一下自己,两个人都不知道的互相插一下。
[/Quote]

插一下的路过
wzyzb 2009-07-19
  • 打赏
  • 举报
回复

回帖是一种美德
Snakebite1985 2009-07-19
  • 打赏
  • 举报
回复
同楼上,参见《算法导论》第2版第5章的 RANDOM-PERMUTATION 算法
zzmoutmans 2009-07-19
  • 打赏
  • 举报
回复
回帖是一种美德
mylzc 2009-07-19
  • 打赏
  • 举报
回复
用扑克洗牌算法吧
加载更多回复(54)

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧