C++之冒泡排序实现

#include<iostream>
#include<string>

using namespace std;

void sort(int * a, int len) {
	int i, j, q;
	for ( i = 0; i < len-1; i++)
	{
		for (j = 0; j < len - 1; j++)
		{
			if (a[j] > a[j + 1]) {
				q = a[j];
				a[j] = a[j + 1];
				a[j + 1] = q;
			}
		}

	}
}

int main() {
	int a[6] = { 222, 10, 5, 799, 3, 2 };
	sort(a, 6);
	int i;
	for (i = 0; i < 6; i++) {
		cout << a[i] << endl;
	}
	cout << "-----------" << endl;

	//另外种写法,通过sizeof获得数组的长度
	for (i = 0; i < sizeof(a) / sizeof(int); i++) {
		cout << a[i] << endl;
	}

	system("pause");
	return 0;
}
联系邮箱:yang_s1r@163.com 博客园地址:https://www.cnblogs.com/Yang34/
原文地址:https://www.cnblogs.com/Yang34/p/14352699.html