成绩 | 10 | 开启时间 | 2017年11月22日 星期三 10:00 |
折扣 | 0.8 | 折扣时间 | 2017年12月15日 星期五 23:55 |
允许迟交 | 否 | 关闭时间 | 2018年01月8日 星期一 23:55 |
要求根据给定输入,按照课堂给定的快速排序算法进行排序,输出排序结果和median3的返回值。
注:1,cutoff值为5,不足cutoff使用插入排序。
2,输入、输出格式参见测试用例0。
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
#include<stdio.h>
#define Cutoff 5
int a[1000];
int num[1000];
int y;
void Swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int Median3(int A[], int Left, int Right)
{
int Center = (Left + Right) / 2;
if (A[Left] > A[Center])
Swap(&A[Left], &A[Center]);
if (A[Left] > A[Right])
Swap(&A[Left], &A[Right]);
if (A[Center] > A[Right])
Swap(&A[Right], &A[Center]);
Swap(&A[Center], &A[Right - 1]);
a[y++] = A[Right - 1];
return A[Right - 1];
}
void Insertation(int A[], int N)
{
int j, p;
int tmp;
for (p = 1; p < N; p++)
{
tmp = A[p];
for (j = p; j>0 && A[j - 1] > tmp; j--)
A[j] = A[j - 1];
A[j] = tmp;
}
}
void Qsort(int A[],int Left,int Right)
{
int i, j;
int Pivot;
if (Left + Cutoff <= Right)
{
Pivot = Median3(A, Left, Right);
i = Left; j = Right - 1;
for (;;)
{
while (A[++i] < Pivot){}
while (A[--j] > Pivot){}
if (i < j)
Swap(&A[i], &A[j]);
else
break;
}
Swap(&A[i], &A[Right - 1]);
Qsort(A, Left, i - 1);
Qsort(A, i + 1, Right);
}
else
Insertation(A + Left, Right - Left + 1);
}
int main()
{
//freopen("1.txt", "r", stdin);
int i = 0; int value; int j = 0; int k = 0;
while (scanf("%d",&value))
{
if (value == '#')
break;
num[i] = value;
i++;
}
if (i > Cutoff)
Qsort(num, 0, i - 1);
else
Insertation(num, i);
printf("After Sorting:\n");
while (j<i)
{
printf("%d ", num[j]);
j++;
}
printf("\nMedian3 Value:\n");
if (y>0)
{
while (k < y)
{
printf("%d ", a[k++]);
}
printf("\n");
}
else
printf("none\n");
return 0;
}