写一个快速排序C语音代码
时间: 2023-05-24 21:04:55 浏览: 96
#include <stdio.h>
void quicksort(int[], int, int);
int main() {
int n, i;
printf("Enter the size of array: ");
scanf("%d", &n);
int a[n];
printf("Enter %d elements: ", n);
for (i=0; i<n; i++)
scanf("%d", &a[i]);
quicksort(a, 0, n-1);
printf("Sorted array is: ");
for (i=0; i<n; i++)
printf("%d ", a[i]);
return 0;
}
void quicksort(int a[], int l, int h) {
int i, j, pivot, temp;
if (l < h) {
pivot = l;
i = l;
j = h;
while (i < j) {
while (a[i] <= a[pivot] && i <= h)
i++;
while (a[j] > a[pivot])
j--;
if (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[pivot];
a[pivot] = a[j];
a[j] = temp;
quicksort(a, l, j-1);
quicksort(a, j+1, h);
}
}
阅读全文