From 4667db6985fd606f35a721b7919aba0007419690 Mon Sep 17 00:00:00 2001 From: huihut Date: Sun, 11 Feb 2018 00:30:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BF=AB=E9=80=9F=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/QuickSort.h | 29 +++++++++++++++++++++++++++++ README.md | 4 +++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Algorithm/QuickSort.h diff --git a/Algorithm/QuickSort.h b/Algorithm/QuickSort.h new file mode 100644 index 0000000..3959d65 --- /dev/null +++ b/Algorithm/QuickSort.h @@ -0,0 +1,29 @@ +// 快速排序 +void QuickSort(vector& v, int low, int high) { + if (low >= high) // 结束标志 + return; + int first = low; // 低位下标 + int last = high; // 高位下标 + float key = v[first]; // 设第一个为基准 + + while (first < last) + { + // 将比第一个小的移到前面 + while (first < last && v[last] >= key) + last--; + if (first < last) + v[first++] = v[last]; + + // 将比第一个大的移到后面 + while (first < last && v[first] <= key) + first++; + if (first < last) + v[last--] = v[first]; + } + // 基准置位 + v[first] = key; + // 前半递归 + QuickSort(v, low, first - 1); + // 后半递归 + QuickSort(v, first + 1, high); +} \ No newline at end of file diff --git a/README.md b/README.md index 0aa8684..f6675d9 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,9 @@ * [冒泡排序](Algorithm/BubbleSort.h) * [冒泡排序(改进版)](Algorithm/BubbleSort_orderly.h) -* [选择排序](Algorithm/BubbleSort_orderly.h) +* [选择排序](Algorithm/SelectionSort.h) +* [快速排序](Algorithm/QuickSort.h) + ## Problems