- Description
给定输入排序元素数目n和相应的n个元素,写出程序,利用内排序算法中直接插入排序算法进行排序,并输出排序过程中每趟及最后结果的相应序列。
- Input
共两行,第一行给出排序元素数目n,第二行给出n个元素,1≤n≤400,每个元素值范围为 [0,100000)
- Output
三个部分:
第1部分为两行,第1行输出文字“Source:”,第2行给出原始序列;
第2部分,开始输出文字“Insert Sort:”,后续输出简单选择排序过程;
第3部分,开始输出文字“Result:”,后续输出排序结果。
- Sample Input
7
48 36 68 72 12 48 2
- Sample Output
Source:
(48) 36 68 72 12 48 2
Insert Sort:
(36 48) 68 72 12 48 2
(36 48 68) 72 12 48 2
(36 48 68 72) 12 48 2
(12 36 48 68 72) 48 2
(12 36 48 48 68 72) 2
(2 12 36 48 48 68 72)
Result:
(2 12 36 48 48 68 72)
#include<stdio.h>
#include<stdlib.h>
int cmp(const int*a,const*b)
{
return *a-*b;
}
int main()
{
int n,*a,i,j;
scanf("%d",&n);
a=(int*)malloc(4*n);
for(i=0;i<n;i++)
scanf("%d",a+i);
printf("Source:\n(%d) ",a[0]);
for(j=1;j<n-1;j++)
printf("%d ",a[j]);
printf("%d\nInsert Sort:\n",a[n-1]);
for(i=1;i<n;i++)
{
qsort(a,i+1,4,cmp);
printf("(");
for(j=0;j<i;j++)
printf("%d ",a[j]);
printf("%d)",a[j]);
if(j==n-1)printf("\n");
else
{for(j++;j<n-1;j++)
printf(" %d",a[j]);
printf(" %d\n",a[n-1]);}
}
printf("Result:\n(");
for(j=0;j<n-1;j++)
printf("%d ",a[j]);
printf("%d)\n",a[n-1]);
free(a);
return 0;
}