#include <stdio.h>
void quickSort(int* arr,int i,int j);
int main(){
int arr[] = {2,1,4,3,6,5,9,4},i;
int len = sizeof(arr)/sizeof(arr[0]);
quickSort(arr,0,len-1);
for(i=0; i<len; i++){
printf("%d ",arr[i]);
}
return 0;
}
void quickSort(int* arr,int i,int j){
if(i>=j) return;
int left = i, right = j;
int cur = arr[left];
while(left<right){
while(left<right && arr[right]>=cur) right--;
while(left<right && arr[left]<=cur) left++;
if(left<right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
arr[i] = arr[left];
arr[left] = cur;
quickSort(arr,i,left-1);
quickSort(arr,left+1,j);
}
C语言快速排序
于 2023-03-06 16:52:51 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)