C++:冒泡排序,代码实现

#include
using namespace std;
int main() {
	int arr[] = { 10,45,2,65,5,8,4,82,11,51 };
	int num = sizeof(arr) / sizeof(arr[0]);
	cout << "排序前" << endl;
	for (int i = 0; i < num; i++) {
		cout << arr[i]<<" ";
	}
	cout << endl;
		for (int i = 0; i < num-1; i++) {
			for (int j = 0; j < num - i - 1; j++) {
				if (arr[j] > arr[j + 1]) {
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
	}
	cout << "排序后" << endl;
	for (int i = 0; i < num; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

C++:冒泡排序,代码实现_第1张图片

你可能感兴趣的:(c++,算法,数据结构)