第十三周項目4――數(shù)組的排序(冒泡法)
來源:程序員人生 發(fā)布時間:2014-12-22 08:19:16 閱讀次數(shù):2434次
/*
* Copyright (c) 2014, 煙臺大學計算機學院
* All rights reserved.
* 文件名稱:test.cpp
* 作 者:李曉凱
* 完成日期:2014年 11 月 29日
* 版 本 號:v1.0
*
* 問題描寫:降序排列數(shù)組的數(shù)并輸出
* 輸入描寫:
* 程序輸出:降序排列數(shù)組的數(shù)并輸出
*/
#include <iostream>
using namespace std;
void bubble_sort(int a[],int n);
void output_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,20); //用冒泡法按降序排序a中元素
output_array(a,20); //輸出排序后的數(shù)組
bubble_sort(b,15); //用冒泡法按降序排序b中元素
output_array(b,15); //輸出排序后的數(shù)組
return 0;
}
//請在下面定義bubble_sort和output_array函數(shù)
void bubble_sort(int c[],int n)
{
int t,i,j;
for(i=0; i<n; ++i)
{
for(j=0; j<n⑴; ++j)
{
if(c[j]<c[j+1])
{
t=c[j+1];
c[j+1]=c[j];
c[j]=t;
}
}
}
cout<<endl;
return ;
}
void output_array(int c[],int n)
{
for(int i=0; i<n; ++i)
{
cout<<c[i]<<" ";
}
return ;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈