数据结构-排序-冒泡排序(c++实现)

#include<iostream>
#include<cstdio>
using namespace std;
int a[100];
void swap(int* c, int* d)
{
	int* temp;
	temp = c;
	c = d;
	d = temp;
}
void bubblesort(int a[],int n)
{
	for(int i=n-1 ; n>=0 ; n--)
		for (int j = 0; j < i; j++) {
			if (a[j] > a[j + 1]) {
				swap(a[j], a[j + 1]);
			}
		}
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int k = 0; k < n;k++) {//输出未排序的值
		cout << a[k]<<' ';
	}
	cout << endl;
	bubblesort(a, n);
	for (int k = 0; k < n; k++) {//输出排序后的值
		cout << a[k] << ' ';
	}

}

猜你喜欢

转载自blog.csdn.net/qq_43710881/article/details/105894687