您的位置:首页 > 其它

基本排序算法--冒泡排序

2015-08-25 20:36 274 查看
#include<iostream>
using namespace std;

void BubbleSort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (a[j]>a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

int main()
{
int array[] = { 34, 65, 12, 43, 67, 5, 78, 10, 3, 70 };
int len = sizeof(array) / sizeof(int);
cout << "the original array are:" << endl;
for (int k = 0; k < len; k++)
cout << array[k] << " ";
cout << endl;
BubbleSort(array, len);
cout << "The sorted array are:" << endl;
for (int k = 0; k<len; k++)
cout << array[k] << ",";
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: