实现直接插入排序算法

/**
*    实验题目:
*        实现直接插入排序算法
*    实验目的:
*        领会直接插入排序的过程和算法设计
*    实验内容:
*        设计程序,实现直接插入排序算法。用相关数据进行测试,并
*    输出各趟的排序结果。
*/

#include

#define MAX_LEN     (100)                       //  最大长度

typedef int key_type;                           //  定义关键字类型为int
typedef char info_type;
typedef struct
{
    key_type key;                               //  关键字项
    info_type data;                             //  其他数据项,类型为info_type
}rec_type;                                      //  查找元素的类型

/*-----------------x和y交换------------------*/
void swap_rec(rec_type &x, rec_type &y)         //  引用类型
{
    rec_type tmp = x;
    x = y;
    y = tmp;
}

/*-----------------创建顺序表------------------*/
void create_list(rec_type recs[], key_type keys[], int n)
{
    int i;

    for(i = 0; i < n; i++)                      // recs[0...n-1]存放排序记录
        recs[i].key = keys[i];
}

/*-----------------输出顺序表------------------*/
void disp_list(rec_type recs[], int n)
{
    int i;

    for(i = 0; i < n; i++)
        printf("%d ", recs[i].key);

    printf("\n");
}

/*-----------------以下运算针对堆排序的程序------------------*/
/*-----------------创建顺序表------------------*/
void create_list1(rec_type recs[], key_type keys[], int n)
{
    int i;

    for(i = 1; i <= n; i++)                     // recs[1...n]存放排序记录
    {
        recs[i].key = keys[i - 1];
    }
}

/*-----------------输出顺序表------------------*/
void disp_list1(rec_type recs[], int n)
{
    int i;

    for(i = 1; i <= n; i++)
    {
        printf("%d ", recs[i].key);
    }
    printf("\n");
}

/*-----------------对recs[0...n-1]按递增有序进行直接插入排序---------------------*/
static void insert_sort(rec_type recs[], int n)
{
    int i, j;
    rec_type tmp;

    for(i = 1; i < n; i++)
    {
        printf("  i = %d, 插入%d, 插入结果: ", i, recs[i].key);
        if(recs[i].key < recs[i - 1].key)           //  反序时
        {
            tmp = recs[i];
            j = i - 1;
            do                                      //  找recs[i]的插入位置
            {
                recs[j + 1] = recs[j];              //  将关键字大于recs[i].key的记录后移
                j--;
            }while(j >= 0 && recs[j].key > tmp.key);
            recs[j + 1] = tmp;                      //  在j + 1处插入recs[i]
        }
        disp_list(recs, n);
    }
}

int main(int argc, char *argv[])
{
    int n = 10;
    key_type a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    rec_type recs[MAX_LEN];

    create_list(recs, a, n);
    printf("排序前: ");
    disp_list(recs, n);
    insert_sort(recs, n);
    printf("排序后: ");
    disp_list(recs, n);

    return 0;
}
测试结果:

排序前: 9 8 7 6 5 4 3 2 1 0
  i = 1, 插入8, 插入结果: 8 9 7 6 5 4 3 2 1 0
  i = 2, 插入7, 插入结果: 7 8 9 6 5 4 3 2 1 0
  i = 3, 插入6, 插入结果: 6 7 8 9 5 4 3 2 1 0
  i = 4, 插入5, 插入结果: 5 6 7 8 9 4 3 2 1 0
  i = 5, 插入4, 插入结果: 4 5 6 7 8 9 3 2 1 0
  i = 6, 插入3, 插入结果: 3 4 5 6 7 8 9 2 1 0
  i = 7, 插入2, 插入结果: 2 3 4 5 6 7 8 9 1 0
  i = 8, 插入1, 插入结果: 1 2 3 4 5 6 7 8 9 0
  i = 9, 插入0, 插入结果: 0 1 2 3 4 5 6 7 8 9
排序后: 0 1 2 3 4 5 6 7 8 9

 

你可能感兴趣的:(数据结构与算法)