快速排序算法C++代码
时间: 2023-08-31 13:40:25 浏览: 94
### 回答1:
以下是快速排序算法的C语言代码:
```
#include <stdio.h>
void quick_sort(int[], int, int);
int partition(int[], int, int);
int main() {
int a[] = {7, 2, 8, 1, 4, 9, 5};
int n = sizeof(a) / sizeof(a[0]);
quick_sort(a, 0, n - 1);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
void quick_sort(int a[], int l, int r) {
int j;
if (l < r) {
j = partition(a, l, r);
quick_sort(a, l, j - 1);
quick_sort(a, j + 1, r);
}
}
int partition(int a[], int l, int r) {
int pivot, i, j, t;
pivot = a[l];
i = l;
j = r + 1;
while (1) {
do
++i;
while (a[i] <= pivot && i <= r);
do
--j;
while (a[j] > pivot);
if (i >= j)
break;
t = a[i];
a[i] = a[j];
a[j] = t;
}
t = a[l];
a[l] = a[j];
a[j] = t;
return j;
}
```
该代码实现了快速排序算法,将数组递归地分割为较小的部分,并对它们进行排序。
### 回答2:
快速排序算法是一种常用的排序算法,通过分治的思想进行排序。
快速排序的基本思想是选取一个基准元素(一般选择第一个元素),将待排序数组分为两部分,使得左边的元素都小于等于基准元素,右边的元素都大于基准元素。然后分别对左右两部分进行递归排序,最终得到有序数组。
以下是快速排序算法的C代码实现:
```c
// 快速排序函数
void quickSort(int arr[], int low, int high) {
if (low < high) {
int i = low, j = high, pivot = arr[low]; // 选取第一个元素作为基准元素
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < pivot) {
i++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = pivot; // 将基准元素放到正确的位置上
// 递归调用快速排序函数
quickSort(arr, low, i - 1);
quickSort(arr, i + 1, high);
}
}
// 测试快速排序算法
int main() {
int arr[] = {7, 2, 1, 6, 8, 5, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("排序后的数组:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
以上代码中,我们定义了一个`quickSort`函数用于进行快速排序,它接受一个数组和数组的起始位置和结束位置作为参数。`main`函数中我们使用一个测试数组来调用`quickSort`函数,并输出排序后的结果。
快速排序算法的时间复杂度为O(nlogn),其中n为待排序数组的长度。
### 回答3:
快速排序(Quick Sort)是一种常用的排序算法,其基本思想是通过一轮划分(partition)将待排序序列分为两部分,其中一部分的所有元素都比另一部分的元素要小。然后对两部分分别进行递归,直到每个部分只有一个元素。具体的C代码如下:
```c
#include <stdio.h>
// 交换数组中两个元素的值
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// 将数组中的元素按照枢轴值划分为两部分
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // 选取最后一个元素作为枢轴值
int i = low - 1; // i代表小于枢轴值的元素的最后一个位置
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]); // 将小于枢轴值的元素放到前面
}
}
swap(&arr[i + 1], &arr[high]); // 将枢轴值放到最后一个小于枢轴值的元素后面
return i + 1; // 返回枢轴值的位置
}
// 快速排序函数
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high); // 进行一轮划分
quickSort(arr, low, pivot - 1); // 对枢轴值左侧的子序列进行递归排序
quickSort(arr, pivot + 1, high); // 对枢轴值右侧的子序列进行递归排序
}
}
// 测试
int main() {
int arr[] = {3, 9, 2, 1, 5, 4, 7, 8, 6};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("排序后的数组为:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
以上是快速排序算法的C代码实现。程序首先定义了一个辅助函数`swap`用于交换数组中两个元素的值。然后使用`partition`函数将数组按照枢轴值划分为两部分,并返回枢轴值的位置。最后,使用`quickSort`函数进行递归排序。在`main`函数中进行了测试,输出排序后的数组。运行该程序可以得到排序结果为1 2 3 4 5 6 7 8 9。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)