diff --git a/260202/冒泡排序.cpp b/260202/冒泡排序.cpp index 0cc3cd1..4f68052 100644 --- a/260202/冒泡排序.cpp +++ b/260202/冒泡排序.cpp @@ -1,27 +1,27 @@ -#include -using namespace std; - -int main() { - int a[5] = {8,114,24,88,5}; // һ򵥵 - int n = 5; - - // ð߼ - for (int i = 0; i < n - 1; i++) { // ѭƱȽ - for (int j = 0; j < n - 1 - i; j++) { // ڲѭȽ - if (a[j] > a[j + 1]) { // ǰıȺĴ - // λ - int temp = a[j]; - a[j] = a[j + 1]; - a[j + 1] = temp; - } - } - } - - // - cout << ": "; - for (int i = 0; i < n; i++) { - cout << a[i] << " "; - } - - return 0; -} +#include +using namespace std; + +int main() { + int a[5] = {8,114,24,88,5}; // 定义一个简单的数组 + int n = 5; + + // 冒泡排序核心逻辑 + for (int i = 0; i < n - 1; i++) { // 外层循环:控制比较轮数 + for (int j = 0; j < n - 1 - i; j++) { // 内层循环:比较相邻两个数 + if (a[j] > a[j + 1]) { // 如果前面的比后面的大 + // 交换位置 + int max = a[j]; + a[j] = a[j + 1]; + a[j + 1] = max; + } + } + } + + // 输出结果 + cout << "排序结果: "; + for (int i = 0; i < n; i++) { + cout << a[i] << " "; + } + + return 0; +}