2755: DS2025-填空题7
金币值:0
定数:1
时间限制:1.000 s
内存限制:128 M
正确:3
提交:3
正确率:100.00% 命题人:
题目描述
输入 $n$ 个整数,对这 $n$ 个整数进行快速排序。其中函数 $partition$ 实现划分,函数 $quickSort$ 实现快速排序,请从下方“测试代码”复制到右侧答题区并填空。
#include <stdio.h>
int partition(int a[], int low, int high);
void quickSort(int a[], int low, int high);
int main(void){
int n; scanf("%d",&n); int a[n];
for(int i=0;i<n;i++) scanf("%d",&a[i]);
quickSort(a,0,n-1);
for(int i=0;i<n;i++) printf("%d ",a[i]);
return 0;
}
int partition(int a[], int low, int high) {
int i=low+1, j=high, x=a[low];
while (i<=j) {
while(i<=j && a[i]<=x) i++;
/*提交代码开始*/
while(i<=j && a[j]>x) ________;
if(i<=j){int t; t=a[i]; a[i]=a[j]; a[j]=t;}
}
a[low]=a[j]; a[j] = x;
return j;
}
void quickSort(int a[], int low, int high) {
if(low<high) {
int index=partition(a,low,high);
quickSort(a, low, index-1); ________;
/*提交代码结束*/
}
}
测试代码 复制
while(i<=j && a[j]>x) ________;
if(i<=j){int t; t=a[i]; a[i]=a[j]; a[j]=t;}
}
a[low]=a[j]; a[j] = x;
return j;
}
void quickSort(int a[], int low, int high) {
if(low<high) {
int index=partition(a,low,high);
quickSort(a, low, index-1); ________;
输入格式
第一行输入一个整数 $n$;
第二行输入一个 $n$ 个整数,相邻两个整数之间用空格隔开。
输出格式
输出 $n$ 个已经排好序的整数。
输入样例 复制
5
20 40 10 50 30
输出样例 复制
10 20 30 40 50