c語言插入排序表
#include <stdio.h>
#define MAX 255
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();
}