zoukankan      html  css  js  c++  java
  • 折半插入排序

    #include"stdio.h"
    #include"stdlib.h"
    
    typedef struct SqList{
        int * elem;
        int length;
        int MaxSize;
    }SqList;
    
    void CreatList(SqList &L)
    {
        L.elem=(int *)malloc(100 * sizeof(int));
        if(!L.elem)
            exit(0);
        else
        {
            L.length = 0;
            L.MaxSize = 100;
            int i, n;
            printf("please shu ru yuan su ge shu:
    ");
            scanf("%d", &n);
            L.length = n;
            printf("please shu ru yuan su:
    ");
            for(i=1;i<=L.length;i++)
                scanf("%d", &L.elem[i]);
        }
    }
    
    void print(SqList L)
    {
        int i;
        for(i=1;i<=L.length;i++)
            printf("%d ", L.elem[i]);
        printf("
    ");
    }
    
    void InsertSort(SqList &L)         //对顺序表L直接插入排序
    {  
        
        int i, j;
        for(i=2;i<=L.length;++i)
            if(L.elem[i] < L.elem[i-1])
            {
                L.elem[0]=L.elem[i];
                for(j=i-1;L.elem[j]>L.elem[0];--j)
                    L.elem[j+1] = L.elem[j];
                L.elem[j+1] = L.elem[0];
            }
    }
    
    void BInsertSort(SqList &L)       //折半插入排序
    {
          int i, j, low, high, mid;
          for(i=2;i<=L.length;++i)
          {
              L.elem[0] = L.elem[i];
              low = 1;
              high = i-1;
              while(low <= high)
              {
                  mid = (low+high)/2;
                  if(L.elem[0] < L.elem[mid])
                      high = mid-1;
                  else
                      low = mid+1;
              }
              for(j=i-1;j>=high+1;--j)
                  L.elem[j+1] = L.elem[j];
              L.elem[high+1] = L.elem[0];
          }
    }
    
    int main()
    {
         SqList L;
         CreatList(L);
         print(L);
         //InsertSort(L);
         BInsertSort(L);
         print(L);
         return 0;
    }

    插入排序 是一种对原有序列排序的方法,逐步使原序列的有序序列增长

    折半插入排序 是在寻找 当前元素的位置时采取了折半查找的方法

  • 相关阅读:
    12.16省选模拟t2 消防
    12.17省选模拟t3 围豆豆
    12.17省选模拟t1 生日礼物
    CF1322D Reality Show
    winform拖动无边框窗体
    关于ToolTip控件在XP系统中问题
    JDK源代码里面的一个for循环
    IIS5.1 无法运行asp.net网站但可访问静态页的解决方案
    winform窗体去掉标题头部的两种方式
    C# 语法之泛型
  • 原文地址:https://www.cnblogs.com/wc1903036673/p/3500034.html
Copyright © 2011-2022 走看看