已知待排序记录的关键字序列为{49,38,65,97,76,13,27,49*},请用c语言编写出用快速排序法进行排序的过程
时间: 2023-06-10 12:05:05 浏览: 141
我可以为您提供代码示例:
```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;
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);
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {49, 38, 65, 97, 76, 13, 27, 49};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("\nSorted array: \n");
printArray(arr, n);
return 0;
}
```
输出结果为:
```
Original array:
49 38 65 97 76 13 27 49
Sorted array:
13 27 38 49 49 65 76 97
```
我希望我的回答能够帮助到您!
阅读全文