[AIZU ONLINE JUDGE] ALDS1_1_A: Insertion Sort [c]

Problem Link: ALDS1_1_A: Insertion Sort

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_A

[c]
#include<stdio.h>
int main()
{
int n,i,k;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(k=0;k<n;k++){ if(k>0)
printf(" ");
printf("%d",arr[k]);
}
printf("n");
for(i=1;i<n;i++){ int temp,j; temp=arr[i]; j=i-1; while(j>=0 && arr[j]>temp){
arr[j+1]=arr[j];
j–;
}
j++;
arr[j]=temp;
for(k=0;k<n;k++){ if(k>0)
printf(" ");
printf("%d",arr[k]);
}
printf("n");
}
return 0;
}

[/c]

Advertisements