Browse Source

添加冒泡排序

DESKTOP-C21C1Q8\tangs 6 years ago
parent
commit
f196255fab
1 changed files with 37 additions and 0 deletions
  1. 37 0
      sort/BubbleSort/main.cpp

+ 37 - 0
sort/BubbleSort/main.cpp

@@ -0,0 +1,37 @@
+//
+// Created by tangs on 2018/11/18.
+//
+
+#include <iostream>
+
+using namespace std;
+
+void BubbleSort(int A[], int n) {
+    int i, j;
+    bool flag;
+    for (i = 0; i < n - 1; i++) {
+        flag = false;
+        for (j = n - 1; j > i; j--) {
+            if (A[j - 1] > A[j]) {
+                swap(A[j - 1], A[j]);
+                flag = true;
+            };
+        }
+        if (flag == false) {
+            return;
+        }
+    }
+}
+
+int main() {
+    int A1[] = {6, 1, 5, 2, 1, 9, 10, 24, 7, 0};
+    BubbleSort(A1, 10);
+
+    // print:
+    // 0 1 1 2 5 6 7 9 10 24
+    for (int i = 0; i < 10; i++) {
+        cout << A1[i] << " ";
+    }
+    cout << endl;
+    return 0;
+}