Browse Source

插入排序C++11仅供参考

webturing 4 years ago
parent
commit
f9981e16e6
1 changed files with 23 additions and 0 deletions
  1. 23 0
      E.cpp

+ 23 - 0
E.cpp

@@ -0,0 +1,23 @@
+#include<bits/stdc++.h>
+using namespace std;
+
+int main() {
+    for (unsigned n, x; cin >> n >> x;) {
+        if (n == 0 and x == 0)break;
+        vector<int> v(n);
+        for (auto &e:v) {
+            cin >> e;
+        }
+        v.emplace_back(x);
+        for (int i = n - 1; i >= 0; i--) {//插入排序
+            if (v[i] <= v[i + 1])
+                break;
+            swap(v[i], v[i + 1]);
+        }
+        for (auto &e:v) {
+            cout << e << " ";
+        }
+        cout << endl;
+    }
+    return 0;
+}