c语言rand数据随机排序,使用C++随机函数Rand()生成n个数,采用冒泡排序法.选择排序法这两种方法对n个数进行排序...

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

lpljlplj

2013.09.22

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:43%    等级:9

已帮助:568人

#include

#include

#include

using namespace std;void Maopao_sort(int array[] ,int n)

{//冒泡排序

int tmp;

for(int i = 0; i < n-1; i++)

{

for(int j = 0; j < n - i-1; j++)

{

if(array[j] < array[j+1])

{

tmp = array[j+1];

array[j+1] =array[j];

array[j] = tmp;

}

}

}

}void Select_sort(int array[],int n)

{//选择排序

int small;//临时变量寄存器

for(int i=0;i

{

small = i;

for(int j=i+1;j

{

if(array[small] > array[j])

{

small = j;

}

}

if(small!=i)

{

int t = array[small];

array[small]=array[i];

array[i]=t;

}

}

}void main()

{

int num_ary[10];

cout << "原数组顺序:" << endl;

srand((unsigned int) time(0));

for(int i = 0 ; i < sizeof(num_ary)/4 ;i++)

{ num_ary[i] = rand()%50;//随机50之间的数字来 初始化数组num_ary

cout << num_ary[i] << endl;

}

Select_sort(num_ary ,sizeof(num_ary)/4);//选择排序从小到大 cout << "选择排序从小到大:" << endl;

for(int i = 0 ; i < sizeof(num_ary)/4 ;i++)

{

cout << num_ary[i] << " ,";

} cout << endl; Maopao_sort(num_ary ,sizeof(num_ary)/4);//冒泡排序从大到小

cout << "冒泡排序从大到小:" << endl;

for(int i = 0 ; i < sizeof(num_ary)/4 ;i++)

{

cout << num_ary[i] << " ,";

}

}

02分享举报

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值