專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

C++冒泡排序程序

作者:黃波海   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月07日   【字體:

// b02.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。

//這是一個(gè)用c++語(yǔ)言寫的冒泡法排序的程序
 
#include "stdafx.h"
 
 
int _tmain(int argc, _TCHAR* argv[])
{
int arr[]={8,5,45,35,56,31,4326,7865,6678,43};
int len=sizeof(arr)/sizeof(int);
for(int k=0;k<len-1;k++)
{
for(int j=0;j<len-1;j++)//
{
if(arr[j]>arr[j+1])
{
int tem; //中間變量要使用
tem=arr[j+1];
arr[j+1]=arr[j];
arr[j]=tem;
}
}
}
for(int i=0;i<10;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
關(guān)閉窗口

相關(guān)文章