void main()
{
void insert_sort(int arr[],int n);
int a[10]={1,4,2,5,6,4,3,2,9,7},i;
printf("before:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
insert_sort(a,10);
printf("\n");
printf("after:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
}
void insert_sort(int arr[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{ temp=arr[i];j=i-1;
do{
arr[j+1]=arr[j];
j--;
}while(arr[j]>temp&&j>=0);
arr[j+1]=temp;
}
}
https://pic2.zhimg.com/0fa3b22573925c1eb4af0e895c53ec49_r.png
1
firemiles 2015-12-13 13:46:28 +08:00 1
j=i;
do{ j--; arr[j+1]=arr[j]; }while(arr[j]>temp&&j>=0); arr[j+1]=temp; } |
2
lightryanking OP @firemiles 谢谢您的解答
|