// 23.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubbleSort(int a[],int n)
{
//排序;第i趟
for(int i=0;i<n-1;i++)
{
//第i趟,比较次数,此为升序,从数组尾开始比较
for(int j=n-1;j>i;j--){
if(a[j-1]>a[j])
{
int b=a[j-1];
a[j-1]=a[j];
a[j]=b;
}
}
}
}
int main(int argc, char* argv[])
{
int a[10]={10,8,3,7,9,5,4,6,2,11};
bubbleSort(a,10);
for (int n=0;n<10;n++){
cout<<a[n]<<" ";
}
return 0;
}
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubbleSort(int a[],int n)
{
//排序;第i趟
for(int i=0;i<n-1;i++)
{
//第i趟,比较次数,此为升序,从数组尾开始比较
for(int j=n-1;j>i;j--){
if(a[j-1]>a[j])
{
int b=a[j-1];
a[j-1]=a[j];
a[j]=b;
}
}
}
}
int main(int argc, char* argv[])
{
int a[10]={10,8,3,7,9,5,4,6,2,11};
bubbleSort(a,10);
for (int n=0;n<10;n++){
cout<<a[n]<<" ";
}
return 0;
}
本文介绍了一个简单的冒泡排序算法实现,通过C++编程语言展示如何对整数数组进行排序。文章提供了一个完整的代码示例,其中包括了排序过程的详细解释。
39万+

被折叠的 条评论
为什么被折叠?



