基本信息
源码名称:c语言插入排序(042.入门级)
源码大小:1.13KB
文件格式:.c
开发语言:C/C++
更新时间:2019-02-14
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
int R[MAX];
void Insert_Sort(int n)
{ /* 对数组R中的记录R[1..n]按递增序进行插入排序 */
int i,j;
for(i=2;i<=n;i ) /* 依次插入R[2],…,R[n] */
if(R[i]<R[i-1])
{/* 若R[i]大于等于有序区中所有的R,则R[i] */
/* 应在原有位置上 */
R[0]=R[i];j=i-1; /* R[0]是哨兵,且是R[i]的副本 */
do{ /* 从右向左在有序区R[1..i-1]中查找R[i]的插入位置 */
R[j 1]=R[j]; /* 将关键字大于R[i]的记录后移 */
j--;
}while(R[0]<R[j]); /* 当R[i]≥R[j]时终止 */
R[j 1]=R[0]; /* R[i]插入到正确的位置上 */
}
}
main()
{
int i,n;
clrscr();
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i )
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i )
printf("%4d",R[i]);
Insert_Sort(n);
puts("\nThe sequence after insert_sort is:");
for(i=1;i<=n;i )
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getchar();
getchar();
}
#include <stdio.h>
#define MAX 255int R[MAX];
void Insert_Sort(int n)
{ /* 对数组R中的记录R[1..n]按递增序进行插入排序 */
int i,j;
for(i=2;i<=n;i ) /* 依次插入R[2],…,R[n] */
if(R[i]<R[i-1])
{/* 若R[i]大于等于有序区中所有的R,则R[i] */
/* 应在原有位置上 */
R[0]=R[i];j=i-1; /* R[0]是哨兵,且是R[i]的副本 */
do{ /* 从右向左在有序区R[1..i-1]中查找R[i]的插入位置 */
R[j 1]=R[j]; /* 将关键字大于R[i]的记录后移 */
j--;
}while(R[0]<R[j]); /* 当R[i]≥R[j]时终止 */
R[j 1]=R[0]; /* R[i]插入到正确的位置上 */
}
}
main()
{
int i,n;
clrscr();
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i )
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i )
printf("%4d",R[i]);
Insert_Sort(n);
puts("\nThe sequence after insert_sort is:");
for(i=1;i<=n;i )
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getchar();
getchar();
}