整数数组随机生成算法
[python]
#python
import random
array = [random.randint(-100, 100) for _ in range(1000)]
for i in array:
print(i,end=" ")
随机抽取一组不重复的数
Fisher-Yates洗牌算法(Knuth洗牌算法)
时间复杂度优化到了O(n),空间复杂度优化到了O(1)。
void shuffle(int datas[],int length){
for(int i=length-1;i>=0;i--){
int j=rand()%(i+1);
int temp=datas[i];
datas[i]=datas[j];
datas[j]=temp;
}
}
void solve(){
srand(time(0));
int datas[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13, 14, 15, 16, 17, 18, 19, 20};
shuffle(datas,20);
for(int i=0;i<20;i++){
cout<<datas[i]<<" ";
}
}
标签:学习指南,shuffle,int,复杂度,算法,随机,datas
From: https://www.cnblogs.com/taotao123456/p/17788988.html