写一个快速排序C语言函数
时间: 2023-04-09 17:00:46 浏览: 70
好的,我可以回答这个问题。以下是一个快速排序的C语言函数示例:
```c
void quicksort(int arr[], int left, int right) {
int i = left, j = right;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if (left < j) {
quicksort(arr, left, j);
}
if (i < right) {
quicksort(arr, i, right);
}
}
```
这个函数使用了快速排序算法来对一个整数数组进行排序。
相关问题
c语言写一个快速排序的函数
下面是一个用C语言实现的快速排序的函数:
```c
void quicksort(int arr[], int left, int right){
int i, j, temp, pivot;
if (left < right){
pivot = left;
i = left;
j = right;
while (i < j){
while (arr[i] <= arr[pivot] && i < right)
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;
quicksort(arr, left, j - 1);
quicksort(arr, j + 1, right);
}
}
```
该函数接受三个参数:待排序数组 `arr`、左边界 `left` 和右边界 `right`。调用该函数时,将要排序的数组和数组边界传递进去即可。
C语言写一个快速排序的函数
下面是一个用C语言实现的快速排序的函数:
```c
void quicksort(int arr[], int left, int right){
int i, j, temp, pivot;
if (left < right){
pivot = left;
i = left;
j = right;
while (i < j){
while (arr[i] <= arr[pivot] && i < right)
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;
quicksort(arr, left, j - 1);
quicksort(arr, j + 1, right);
}
}
```
该函数接受三个参数:待排序数组 `arr`、左边界 `left` 和右边界 `right`。调用该函数时,将要排序的数组和数组边界传递进去即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)