实现折半插入排序算法

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

#include <stdio.h>

#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 bin_insert_sort(rec_type recs[], int n)
{
    int i;
    int j;
    int low;
    int high;
    int mid;
    rec_type tmp;

    for(i = 1; i < n; i++)
    {
        if(recs[i].key < recs[i - 1].key)           //  反序时
        {
            printf("  i = %d, 插入%d, 插入结果: ", i, recs[i].key);
            tmp = recs[i];                          //  将recs[i]保存到tmp中
            low = 0;
            high = i - 1;
            while(low <= high)
            {
                mid = (low + high) / 2;             //  取中间位置
                if(tmp.key < recs[mid].key)
                    high = mid - 1;                 //  插入点在左半区
                else
                    low = mid + 1;                  //  插入点在右半区
            }

            for(j = i - 1; j >= high + 1; j--)      //  集中进行元素后移
                recs[j + 1] = recs[j];
            recs[high + 1] = tmp;                   //  插入tmp
        }
        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);
    bin_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

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值