帮我写一个vector的快速排序
时间: 2024-04-25 11:03:13 浏览: 37
QuickSort_quicksort代码_快速排序_
好的,以下是一个快速排序的vector实现:
```
#include <iostream>
#include <vector>
using namespace std;
void quicksort(vector<int>& nums, int left, int right) {
if (left < right) {
int pivot = left;
int i = left + 1;
int j = right;
while (i <= j) {
if (nums[i] < nums[pivot] && nums[j] > nums[pivot]) {
swap(nums[i], nums[j]);
i++;
j--;
}
if (nums[i] >= nums[pivot]) {
i++;
}
if (nums[j] <= nums[pivot]) {
j--;
}
}
swap(nums[pivot], nums[j]);
quicksort(nums, left, j - 1);
quicksort(nums, j + 1, right);
}
}
int main() {
vector<int> nums = {4, 1, 7, 3, 9, 2};
quicksort(nums, 0, nums.size() - 1);
for (int num : nums) {
cout << num << " ";
}
cout << endl;
return 0;
}
```
阅读全文