算法1:
当数组个数=1000;生成时间约为16ms。
当数组个数=10000;生成时间约为203ms。
当数组个数=100000;生成时间无限。 因为The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767). Use the srand function to seed the pseudorandom-number generator before calling rand.
那么也就是说:想要生成一个大于32767个元素数组的不重复随机数组则,不能使用算法1.
算法2:
random_shuffle()包含与<algorithm>
当数组个数=100000;生成时间约为15ms
当数组个数=1000000;生成时间约为203ms
当数组个数=10000000;生成时间约为2437ms = 2.4s
当数组个数=100000000(1亿);生成时间约为27687ms = 27.7s
可以看出,算法呈O(n)线性增加
本文对比了两种生成不重复随机数组的算法,分析了它们的时间复杂度及性能表现,指出当数组大小超过一定阈值时,算法效率会显著下降。重点介绍了线性时间复杂度的随机洗牌算法,并提供了适用于大规模数据集的优化建议。
5万+

被折叠的 条评论
为什么被折叠?



