#include <iostream> using namespace std; int main() { int arr[] = { 4,2,8,0,5,7,1,3,9 ,15,17,14 }; cout << "排序前" << endl; for (size_t i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++) { cout << arr[i] << endl; } cout << endl; //开始冒泡排序 //总排序轮数为 元素-1 for (size_t i = 0; i < (sizeof(arr) / sizeof(arr[0])) - 1; i++) { //内层循环对比 次数=元素个数-当前轮数-1 for (size_t j = 0; j < (sizeof(arr) / sizeof(arr[0])) - 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 (size_t i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++) { cout << arr[i] << endl; } system("pause"); return 0; }