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

C++冒泡排序程序

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

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

//這是一個用c++語言寫的冒泡法排序的程序
 
#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)文章