Browse Source

添加选择排序

DESKTOP-C21C1Q8\tangs 6 years ago
parent
commit
592af78800
1 changed files with 32 additions and 0 deletions
  1. 32 0
      sort/SelectSort/main.cpp

+ 32 - 0
sort/SelectSort/main.cpp

@@ -0,0 +1,32 @@
+//
+// Created by tangs on 2018/11/18.
+//
+
+#include <iostream>
+
+using namespace std;
+
+void SelectSort(int A[], int n) {
+    int i, j, min;
+    for (i = 0; i < n - 1; i++) {
+        min = i;
+        for (j = i + 1; j < n; j++)
+            if (A[j] < A[min])
+                min = j;
+        if (min != i)
+            swap(A[i], A[min]);
+    }
+}
+
+int main() {
+    int A1[] = {6, 1, 5, 2, 1, 9, 10, 24, 7, 0};
+    SelectSort(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;
+}