插入排序(一)——直接插入排序(稳定的排序)

 
//文件table.h  头函数,数据类型的声明

#include <stdio.h>
#define MAXSIZE 100  /*文件中记录个数的最大值*/
#define N 5

typedef int keytype; /*定义排序码类型为整型*/typedef struct
{
 keytype key;
     /*此处还可以定义记录中除排序码外的其他域*/
}recordtype;        /*记录类型的定义*/
typedef struct 
{
 recodetype r[MAXSIZE+1];
 int length;/*待排序文件中记录的个数*/
}table; /*待排序文件类型*/
 
 
 
//文件insert_Sort.cpp
#include <stdio.h>
#include "table.h"
void insertsort(table *tab)
{
 int i,j;
 for(i=2;i<=tab->length ;i++)
 {
  j=i-1;
  tab->r[0].key =tab->r.key ;
  while(tab->r[0].key<tab->r[j].key )
  {
   tab->r[j+1].key=tab->r[j].key ;
   j=j-1;
  }
  tab->r[j+1].key =tab->r[0].key ;
 }
}

void main()
 {
  int i; table tab;
 //tab.length=7;
 
 printf("please input the length of the table \\n");
 //printf("\\n");
 scanf("%d",&tab.length );
 tab.r[0].key=0; 
 /*tab.r[1].key=312;*/
 //getchar();//消除回车符 printf("please input the item of the table \\n");
 
 for (i=1;i<=tab.length;i++)
 {
  scanf("%d",&tab.r.key );
  
 }
  printf("the old order \\n");
  for (i=1;i<=tab.length;i++)
 {
  printf("%d\\t",tab.r.key );
  
 }
  
  printf("\\n the new order \\n"); 
 /*table t=*tab;*/
 insertsort(&tab); for (i=1;i<=tab.length;i++)
  {
   /*scanf("%d",&tab->r.key );*/
   printf("%d\\t",tab.r.key );
  }
 
 scanf("%d",&i);
 
 }
原文地址:https://www.cnblogs.com/zhiji6/p/1649303.html