请C语言大牛修改一下我的程序(排序改成快排)

#include<stdio.h>int z[1001];int x[1001][2];int main(){ int q; int w,e,r; int t=0,y=0; scanf("%d",&q); for(w=0;w<q;w++) { scanf("%d",&z[w]); } for(w=0;w<q;w++) { r=0; for(e=0;e<q-1;e++) { if(z[e]>z[e+1]){r=z[e+1];z[e+1]=z[e];z[e]=r;} } } for(w=0;w<q;w++) { if(x[t][0]==z[w]){x[t][1]++;} if(x[t][0]!=z[w]){t++;x[t][0]=z[w];x[t][1]=1;} } for(w=1;w<=t;w++) { printf("%d %d\n",x[w][0],x[w][1]); }}对一楼的回答:我要的是把我程序中的排序改成qsort,而不是整个都改成qsort
2026-12-04 10:59:50
推荐回答(1个)
回答1:

#include

int Partition(int r[],int i,int j) //快速排序的一次划分
{
int temp=r[i];
while(i {
while (i=temp) j--;
if(i {
r[i++]=r[j];
}
while (i if(i { r[j--]=r[i];
}
}
r[i]=temp;
return i;
}
void QuickSort(int r[],int i,int j) //快速排序
{
if(i {
int pivot=Partition(r,i,j);
QuickSort(r,i,pivot-1);
QuickSort(r,pivot+1,j);
}
}

main()
{
int i,q;
int a[1000];
printf("Enter the num of array:\n");
scanf("%d",&q);
printf("Enter the member of array:\n");
for(i=0;i scanf("%d",&a[i]);
QuickSort(a,0,q-1);
printf("QuickSort:\n");
for(i=0;i {
printf("%d ",a[i]);
}
printf("\n");
}

已测试 比如
Enter the num of array:
10
Enter the member of array:
6 3 8 2 5 9 5 2 5 10
输出
QuickSort:
2 2 3 5 5 5 6 8 9 10