雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
2017-12-16 20:20
841 查看
为数组中A每个元素随机赋一个优先级,然后依据优先级P对数组进行排序,详细见算法导论第三版或者网上
伪代码
c++代码
第一次运行结果
第二次运行结果
伪代码
c++代码
#include <iostream> #include<time.h> #define N 20//数组大小为20 void randP(int prior[],int len); void permuteBySorting(int array[],int prior[],int len); using namespace std; int main() { int Array ,Prior ; for(int i=0;i<N;++i)//数组Array循环赋初值 Array[i]=i+1; cout<<"原数组Array的值为:"<<endl; for(int j=0;j<N;++j) cout<<Array[j]<<" "; cout<<endl<<"与数组对应的优先级大小为:"<<endl; randP(Prior,N); permuteBySorting(Array,Prior,N); cout<<endl<<"排序后的数组Array为:"<<endl; for(int i=0;i<N;++i) cout<<Array[i]<<" "; cout<<endl; return 0; } void randP(int prior[],int len)//随机生成优先级 { srand(time(NULL));//rand随机数计算之前需要设置随机种子,不然每次结果一样 for(int i=0;i<len;++i) { prior[i]=rand()%(len*len*len);//取值大尽可能让优先级唯一 cout<<prior[i]<<" "; } } void permuteBySorting(int array[],int prior[],int len)//排序 { int temp1=0;//初始最小 int temp2=0; for(int i=1;i<len;++i) { temp1=prior[i]; temp2=array[i]; int j; for(j=i-1;(j>=0&&prior[j]>temp1);--j) { prior[j+1]=prior[j]; array[j+1]=array[j]; } prior[j+1]=temp1; array[j+1]=temp2; } }
第一次运行结果
第二次运行结果
相关文章推荐
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果