Use Shell sort algorithm and Shell Increment Sequence to sort a sequence in descending orders.
输入格式:
2 lines.
The first line means the number(<=20) of keys.
The second line is consisted of several integers divided by comma.
输出格式:
Several lines.
Each line shows the middle result of shell sort. Add '\n' to the end of each line
输入样例:
在这里给出一组输入。例如:
10
49,38,65,97,76,13,27,50,2,8,
输出样例:
在这里给出相应的输出。例如:
49,38,65,97,76,13,27,50,2,8,
76,97,65,50,49,38,27,13,2,8,
97,76,65,50,49,38,27,13,8,2,
C语言代码
#include<stdio.h>
void printstring(int * A,int N){
int i;
for(i=0;i<N;i++){
printf("%d,",A[i]);
}
printf("\n");
}
void ShellSort(int * A,int N){
int Increment=N/2;
int P,i;
for(;Increment>0;Increment/=2){
for(P=Increment;P<N;P++){
int temp=A[P];
for(i=P;i>=Increment;i-=Increment){
if(temp>A[i-Increment]){
A[i]=A[i-Increment];
}
else break;
}
A[i]=temp;
}
printstring(A,N);
}
}
int main(){
int N;
scanf("%d",&N);
int A[N];
int i;
for(i=0;i<N;i++){
scanf("%d,",&A[i]);
}
ShellSort(A,N);
return 0;
}