第十三周項(xiàng)目4――數(shù)組的排序(冒泡法)
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-12-22 08:19:16 閱讀次數(shù):2427次
/*
* Copyright (c) 2014, 煙臺(tái)大學(xué)計(jì)算機(jī)學(xué)院
* All rights reserved.
* 文件名稱:test.cpp
* 作 者:李曉凱
* 完成日期:2014年 11 月 29日
* 版 本 號(hào):v1.0
*
* 問(wèn)題描寫:降序排列數(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;
}
//請(qǐng)?jiān)谙旅娑xbubble_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)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)