栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

浅析直接插入排序与折半插入排序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浅析直接插入排序与折半插入排序

首先看一下例子,将数据一个个的插入到一个列表中,插入后这个列表就排序好了

注意:这个列表是递增的,而且内存空间已分配好,只是没有填充真正的数据,如下代码:

复制代码 代码如下:
int InsertSort(MergeType *L, int data)
{
 int j;

 if (!L->len)
 {
  L->elem[++L->len] = data;
  return 0;
 }

 for (j = L->len-1; j >= 0; --j)
 {
  if (data < L->elem[j])
  {
   L->elem[j+1] = L->elem[j];
  }
  else
  {
   L->elem[j+1] = data;
   L->len++;
   break;
  }
 }
 return 0;
}

测试用例的代码如下:
复制代码 代码如下:
#define  LISTLEN 10 
 MergeType pList;
 MergeType pT; 

 pList.elem = (int*)malloc(sizeof(int)*LISTLEN);
 ScanfList(&pList);
 pList.len  = LISTLEN;
 pList.size = LISTLEN;
 

 pT.elem = (int*)malloc(sizeof(int)*LISTLEN); 
 pT.len = 0;
 pT.size = LISTLEN;
 memset(pT.elem, 0, LISTLEN*sizeof(int));

 for (int i = 0; i < LISTLEN; i++ )
 {
  InsertSort(&pT, pList.elem[i]);
 }

 PrintList(&pT);

 free(pList.elem);
 free(pT.elem);
 pList.elem = NULL;
 pT.elem = NULL;

其他函数以及代码请定义请参考:冒泡算法的改进

直接插入排序

核心思想:是将一个记录插入到已排序好的有序表中,从中得到一个新的、记录数增1的有序表,也就是说递增的次序排列每一个数据,将每一个数据插入到前面已经排序好的位置中。看下面的代码

复制代码 代码如下:
int InsertSort(MergeType* L)
{
 int i, j = 0;
 int nCompKey;

 if (!L->elem || !L->len) return -1;

 if (L->elem && (L->len==1)) return 0;

 for ( i = 1; i < L->len; i++)
 {
  if (L->elem[i] < L->elem[i-1]) 
  {
   nCompKey = L->elem[i];
   L->elem[i] = L->elem[i-1];   

   //move elements back
   for (j = i-2; j >= 0 && nCompKey < L->elem[j]; --j)
   {
    L->elem[j+1] = L->elem[j];
   }
   L->elem[j+1] = nCompKey;
  }
 }
 return 0;
}

这里从第二个数据开始,比较当前的数据是否小于前面的一个数,如果小于前面一个数据,就将当前数据插入到前面的队列中,在插入到前面数据中的过程,要移动数据

这里要注意时间的复杂度:

总的比较次数=1+2+……+(i+1-2+1)+……+(n-2+1)= n*(n-1)/2= O(n^2)

总的移动次数=2+3+……+(2+i-1)+ …… + n = (n+2)*(n-1)/2=O(n^2)

当然还要考虑空间复杂度:其实这里使用了一个变量的存储空间作为移动数据的临时空间


这里在移动的过程中,可以减少代码理解的复杂度,但会在每一个数据比较的过程中增加一次比较的次数,如下代码:

复制代码 代码如下:
 ...
 if (L->elem[i] < L->elem[i-1]) 
 {
  nCompKey = L->elem[i];
  
  for (j = i-1; j >= 0 && L->elem[j] > nCompKey; --j)
  {
   L->elem[j+1] = L->elem[j];
  }
  L->elem[j+1] = nCompKey;
 }
 ...

在插入数据的过程中,其实前面的数据都已经排序好了,这时候一个个的进行查找,必定查找次数较多,如果采用折半查找算法可以减少次数,如下

复制代码 代码如下:

int BInsertSort(MergeType *L)
{
 int i, j;
 int low, high, mid;
 int nCompKey;

 for (i = 1; i <= L->len - 1; i++ )
 {
  nCompKey = L->elem[i];
  low = 0;
  high = i - 1;

  
  while(low <= high)
  {
   mid = (low + high)/2;
   if ( nCompKey > L->elem[mid] )
   {
    low = mid + 1;
   }
   else
   {
    high = mid -1;
   }
  }

  
  for (j = i-1; j >= high+1; j-- )
  {
   L->elem[j+1] = L->elem[j];
  }
  
  L->elem[high+1] = nCompKey;
 }
 return 0;
}

具体什么原因,请看上面的注释,这里为什么用high+1,但是此时high与low的位置只相差一个位置,才会跳出while循环,请看下面的改进

复制代码 代码如下:
#if 0
  for (j = i-1; j >= high+1; j-- )
  {
   L->elem[j+1] = L->elem[j];
  }
  L->elem[high+1] = nCompKey;
#else
  for (j = i-1; j >= low; j-- )
  {
   L->elem[j+1] = L->elem[j];
  }
  L->elem[low] = nCompKey;
#endif
...

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号