From a7a6e38cac1bfbeba9b6097f6936229771b98683 Mon Sep 17 00:00:00 2001 From: huihut Date: Sun, 11 Feb 2018 00:20:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=89=E6=8B=A9=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Algorithm/BubbleSort.h | 1 - Algorithm/BubbleSort_orderly.h | 8 +++----- Algorithm/SelectionSort.h | 17 +++++++++++++++++ README.md | 3 ++- 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 Algorithm/SelectionSort.h diff --git a/Algorithm/BubbleSort.h b/Algorithm/BubbleSort.h index f899db6..c787fcb 100644 --- a/Algorithm/BubbleSort.h +++ b/Algorithm/BubbleSort.h @@ -1,5 +1,4 @@ // 冒泡排序 - void BubbleSort(vector& v) { int temp; for (int i = 0; i < v.size() - 1; ++i) { diff --git a/Algorithm/BubbleSort_orderly.h b/Algorithm/BubbleSort_orderly.h index 9ecdbb2..5c39409 100644 --- a/Algorithm/BubbleSort_orderly.h +++ b/Algorithm/BubbleSort_orderly.h @@ -1,14 +1,12 @@ -// 冒泡排序(跳过有序的改进版) - +// 冒泡排序(改进版) void BubbleSort_orderly(vector& v) { int temp; bool orderly = false; for (int i = 0; i < v.size() - 1 && !orderly; ++i) { orderly = true; for (int j = 0; j < v.size() - 1 - i; ++j) { - if (v[j] > v[j + 1]) - { - orderly = false; + if (v[j] > v[j + 1]) { // 从小到大 + orderly = false; // 发生交换则仍非有序 temp = v[j]; v[j] = v[j + 1]; v[j + 1] = temp; diff --git a/Algorithm/SelectionSort.h b/Algorithm/SelectionSort.h new file mode 100644 index 0000000..d9c7b1e --- /dev/null +++ b/Algorithm/SelectionSort.h @@ -0,0 +1,17 @@ +// 选择排序 +void SelectionSort(vector& v) { + int min, temp; + for (int i = 0; i < v.size() - 1; ++i) { + min = i; + for (int j = i + 1; j < v.size(); ++j) { + if (v[j] < v[min]) { // 标记最小的 + min = j; + } + } + if (i != min) { // 交换到前面 + temp = v[i]; + v[i] = v[min]; + v[min] = temp; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 908b077..0aa8684 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ ### 排序 * [冒泡排序](Algorithm/BubbleSort.h) -* [冒泡排序(跳过有序的改进版)](Algorithm/BubbleSort_orderly.h) +* [冒泡排序(改进版)](Algorithm/BubbleSort_orderly.h) +* [选择排序](Algorithm/BubbleSort_orderly.h) ## Problems