实现直接插入排序算法

来源:互联网 发布:投影仪突然连不上网络 编辑:程序博客网 时间:2024/04/16 13:48

/*exp11-1.cpp*/
#include <stdio.h>
#define MAXE 20/*线性表中最多元素个数*/
typedef int KeyType;
typedef char InfoType[10];
typedef struct
{
 KeyType key;/*记录类型*/
 InfoType data;/*其他数据项,类型为InfoType*/
}RecType;

void InsertSort(RecType R[],int n)/*对R[0...n-1]按递增有序进行直接插入排序*/
{
 int i,j,k;
 RecType temp;
 for(i=1;i<n;i++)
 {
  temp=R[i];
  j=i-1;/*从右向左在有序区R[0...n-1]中找R[i]的插入位置*/
        while(j>=0 && temp.key<R[j].key)
  {
   R[j+1]=R[j];/*将关键字大于R[i].key的记录后移*/
   j--;
  }
  R[j+1]=temp;/*在j+1处插入R[i]*/
  printf("   i=%d    ",i);/*输出每一趟的排序结果*/
        for(k=0;k<n;k++)
   printf("%3d",R[k].key);
  printf("\n");
 }
}

void main()
{
 int i,k,n=10;
 KeyType a[]={9,8,7,6,5,4,3,2,1,0};
 RecType R[MAXE];
 for(i=0;i<n;i++)
  R[i].key=a[i];
 printf("\n");
 printf("初始关键词");
 for(k=0;k<n;k++)/*输出初始关键词序列*/
  printf("%3d",R[k].key);
 printf("\n");
 InsertSort(R,n);
 printf("最后结果  ");/*输出初始关键词序列*/
 for(k=0;k<n;k++)
  printf("%3d",R[k].key);
 printf("\n\n");
}

原创粉丝点击