c++实现双指针实现的快速排序代码
时间: 2023-12-03 22:41:05 浏览: 95
以下是C++实现双指针实现的快速排序代码:
```cpp
#include <iostream>
using namespace std;
void quickSort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right, pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= pivot) {
i++;
}
arr[j] = arr[i];
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
int main() {
int arr[] = { 5, 3, 8, 4, 2, 7, 1, 10 };
int len = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, len - 1);
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
return 0;
}
```
阅读全文