- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #define N 20
- void cr(int *,int *,int *); //插入
- void crpx(int *); //插入排序
- void dy(int *); //打印
- void main()
- {
- int arr[N];
- srand(time(NULL));
- puts("初始化数组");
- for(int i=0;i<N;i++)
- {
- arr[i]=rand()%50;
- }
- dy(arr);
- crpx(arr);
- puts("插入排序后数组");
- dy(arr);
- system("pause");
- }
- void cr(int *small,int *big,int *a) //递归插入
- {
- if(*small<=*big)
- return;
- else
- {
- char ch=*big;
- *big=*small;
- *small=ch;
- if(small==a)
- return;
- else
- cr(small-1,small,a);
- }
- }
- void crpx(int *a)
- {
- int *p=a+1;
- while(p<a+N)
- {
- cr(p-1,p,a);
- p++;
- }
- }
- void dy(int *a)
- {
- for(int i=0;i<N;i++)
- {
- printf("%d\\t",a[i]);
- if(i+1%10==0)
- putchar('\\n');
- }
- putchar('\\n');
- }
- //该片段来自于http://www.codesnippet.cn/detail/250920136131.html
来源: http://www.codesnippet.cn/detail/250920136131.html