/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: array.cpp
* 作 者:徐本锡
* 完成日期:2012 年11 月29 日
* 版本号: v1.0
*
* 输入描述:无
* 问题描述:冒泡法排列数组
*/
#include <iostream>
using namespace std;
//两个函数 bubble_sort 和 output_array 的声明
void bubble_sort(int a[],int b);//声明降序排列数组的函数
void output_array(int a[],int b);//声明输出数组的函数
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,20); //用冒泡法按降序排序 a中元素
output_array(a,20); //输出排序后的数组
cout<<endl;
bubble_sort(b,15); //用冒泡法按降序排序 b中元素
output_array(b,15); //输出排序后的数组
cout<<endl;
return 0;
}
//下面定义 bubble_sort 和 output_array 函数
void bubble_sort(int a[],int b)//定义降序排列数组的函数
{
int i,j,num;
for(i=1;i<b;++i)
{
for(j=0;j<b-i;++j)
{
if(a[j]<=a[j+1])
{
num=a[j+1];
a[j+1]=a[j];
a[j]=num;
}
}
}
return;
}
void output_array(int a[],int b)//定义输出数组的函数
{
int i;
for(i=0;i<b;++i)
cout<<a[i]<<'\t';
return;
}