c++实现基于分治策略的快速排序算法代码
时间: 2023-06-04 12:03:52 浏览: 97
好的,我可以帮您实现基于分治策略的快速排序算法。以下是代码实现示例:
```c
#include <stdio.h>
void quick_sort(int array[], int left, int right) {
if (left < right) {
int i = left, j = right, pivot = array[left];
while (i < j) {
while (i < j && array[j] >= pivot) j--;
if (i < j) array[i++] = array[j];
while (i < j && array[i] < pivot) i++;
if (i < j) array[j--] = array[i];
}
array[i] = pivot;
quick_sort(array, left, i - 1);
quick_sort(array, i + 1, right);
}
}
int main() {
int array[] = {6, 5, 3, 1, 8, 7, 2, 4};
int n = sizeof(array) / sizeof(int);
quick_sort(array, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
return 0;
}
```
希望能对您有帮助。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)