使用随机函数初始化一个数组,并对数组进行排序(冒泡排序法和选择排序法)
来源:互联网 发布:licecap for windows 编辑:程序博客网 时间:2024/05/12 09:01
使用C++随机函数Rand()生成n个数,采用冒泡排序法.选择排序法这两种方法对n个数进行排序,要求用函数来实现。
[ 标签:c++,随机函数,rand ] 绿蓑衣(Lian 回答:1 人气:1 解决时间:2010-12-23 09:24
满意答案好评率:100% #include <iostream>
#include <string>
#include <ctime>
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<n-1;i++)
{
small = i;
for(int j=i+1;j<n;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] << " ,";
}
}
[ 标签:c++,随机函数,rand ] 绿蓑衣(Lian 回答:1 人气:1 解决时间:2010-12-23 09:24
满意答案好评率:100% #include <iostream>
#include <string>
#include <ctime>
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<n-1;i++)
{
small = i;
for(int j=i+1;j<n;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] << " ,";
}
}
- 使用随机函数初始化一个数组,并对数组进行排序(冒泡排序法和选择排序法)
- 使用指针法对数组进行冒泡排序、选择排序、插入排序法、二分查找法
- 对随机数组进行冒泡排序
- 冒泡排序 - 随机生成数组并对其排序
- 冒泡法对数组进行排序
- C#冒泡法对数组进行排序
- 用java写一个对数组进行冒泡排序法
- 利用随机类生成数组,并用冒泡排序以及选择排序对不同数组的元素进行从小到大的排序
- 冒泡排序算法----对一个数组进行冒泡排序
- 指针对数组排序选择法和冒泡法
- 用冒泡和选择法对两个数组排序
- 使用for循环对数组进行排序[冒泡法]
- 选择法对数组进行排序
- 选择法对整形数组进行排序
- 利用冒泡排序法,对任意数组进行排序;
- 使用冒泡对数组进行排序
- 冒泡法进行数组排序
- 选择排序算法---对一个数组进行选择排序
- CRichEditCtrl 使用技巧
- The APR based Apache Tomcat Native library which allows optimal performance in p
- 近期面试题汇总
- MapReduce: 一个巨大的倒退(zz)
- 数据表的连接
- 使用随机函数初始化一个数组,并对数组进行排序(冒泡排序法和选择排序法)
- 让Android程序支持安装到SD卡,降低卸载率
- 第八周任务(一)3
- Sorenson Spark视频编解码器
- 《ITIL 2011介绍、解析》PPT下载,录音下载
- 几位数乘法的结构
- 上传下载Cisco路由器配置文件
- POJ—1061 青蛙的约会 解题报告(超详细)
- 第八周实验报告1