|
@@ -45,3 +45,40 @@ int main() {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
+# 快速排序
|
|
|
+
|
|
|
+```cpp
|
|
|
+#include <bits/stdc++.h>
|
|
|
+using namespace std;
|
|
|
+
|
|
|
+void quickSort(vector<int>& nums, int left, int right) {
|
|
|
+ if (left >= right) return;
|
|
|
+ int temp = nums[left];
|
|
|
+ int i = left;
|
|
|
+ int j = right;
|
|
|
+ while (i != j) {
|
|
|
+ while (j > i && nums[j] >= temp) {
|
|
|
+ j--;
|
|
|
+ }
|
|
|
+ while (j > i && nums[i] <= temp) {
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ if (i < j) {
|
|
|
+ swap(nums[i], nums[j]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ swap(nums[left], nums[j]);
|
|
|
+ quickSort(nums, left, i - 1);
|
|
|
+ quickSort(nums, i + 1, right);
|
|
|
+}
|
|
|
+
|
|
|
+int main() {
|
|
|
+ vector<int> nums = {50, 10, 20, 30, 70, 40, 80, 60};
|
|
|
+ quickSort(nums, 0, nums.size() - 1);
|
|
|
+ for (auto num : nums) {
|
|
|
+ cout << num << " ";
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+```
|
|
|
+
|