您的位置:首页 > 其它

第十三周项目4 数组排序

2014-11-23 10:39 267 查看
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2014年11月20日
*版本号:v1.0
*
*问题描述:数组排序
*输入描述:无
*程序输出:数组排序
*/
#include <iostream>
using namespace std;
void bubble_sort(int a[],int n);
void butput_array(int a[],int n);
int main()
{
int a[20]= {86,76,62,58,77,85,92,80,96,88,77,67,80,68,88,87,64,59,61,76};
int b[15]= {27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};
bubble_sort(a,18);
cout<<endl;
butput_array(a,18);
cout<<endl;
bubble_sort(b,10);
cout<<endl;
butput_array(b,10);
cout<<endl;
return 0;
}
void bubble_sort(int a[],int n)
{
int i,j,t;
for(j=0; j<n; ++j)
{
for(i=0; i<n-j-1; ++i)
if(a[i]<=a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
cout<<"用冒泡法降序排序为:"<<endl;
for(i=0; i<n; ++i)
cout<<a[i]<<" ";
}
void butput_array(int a[],int n)
{
int x,y,z;
for(x=0; x<n-1; ++x)
for(y=0; y<n-1; ++y)
if(a[y]>a[y+1])
{
z=a[y];
a[y]=a[y+1];
a[y+1]=z;
}
cout<<"排序后的数组为:"<<endl;
for(y=0; y<n; ++y)
cout<<a[y]<<" ";
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: