//功能:设计一个函数,实现一个一维数组的元素按大小降序排列,主程序中输入10个整数,要求降序显示 |
#include <iostream> |
#include <iomanip> |
using namespace std; |
void Swap( int & m, int & n) //交换函数 |
{ |
int temp; |
temp=m; |
m=n; |
n=temp; |
} |
void BubbleSort( int a[], int n) //降序函数 |
{ |
int i,j; |
for (i=0;i<n;i++) |
{ |
for (j=1;j<n-i;j++) |
{ |
if (a[j-1]<a[j]) |
{ |
Swap(a[j-1],a[j]); |
} |
} |
} |
} |
int main() |
{ |
//输入 |
int i,a[10]; |
cout<< "请输入10个整数:" <<endl; |
for (i=0;i<10;i++) |
{ |
cin>>a[i]; |
} |
//调用函数 |
BubbleSort(a, 10); |
|
//输出 |
for (i=0;i<10;i++) |
{ |
cout<<setw(5)<<a[i]; |
} |
cout<<endl; |
return 0; |
} |