国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

淺析直接插入排序與折半插入排序

2020-01-26 15:39:10
字體:
供稿:網(wǎng)友

首先看一下例子,將數(shù)據(jù)一個個的插入到一個列表中,插入后這個列表就排序好了

注意:這個列表是遞增的,而且內(nèi)存空間已分配好,只是沒有填充真正的數(shù)據(jù),如下代碼:

復(fù)制代碼 代碼如下:

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];/*數(shù)據(jù)后移*/
  }
  else
  {
   L->elem[j+1] = data;
   L->len++;
   break;
  }
 }
 return 0;
}

測試用例的代碼如下:

復(fù)制代碼 代碼如下:

#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;

其他函數(shù)以及代碼請定義請參考:冒泡算法的改進

直接插入排序

核心思想:是將一個記錄插入到已排序好的有序表中,從中得到一個新的、記錄數(shù)增1的有序表,也就是說遞增的次序排列每一個數(shù)據(jù),將每一個數(shù)據(jù)插入到前面已經(jīng)排序好的位置中??聪旅娴拇a

復(fù)制代碼 代碼如下:

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])  /*第二個數(shù)據(jù)比第一個數(shù)據(jù)小*/
  {
   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) /*在>=退出當(dāng)前循環(huán)*/
   {
    L->elem[j+1] = L->elem[j];
   }
   L->elem[j+1] = nCompKey;
  }
 }
 return 0;
}

這里從第二個數(shù)據(jù)開始,比較當(dāng)前的數(shù)據(jù)是否小于前面的一個數(shù),如果小于前面一個數(shù)據(jù),就將當(dāng)前數(shù)據(jù)插入到前面的隊列中,在插入到前面數(shù)據(jù)中的過程,要移動數(shù)據(jù)

這里要注意時間的復(fù)雜度:

總的比較次數(shù)=1+2+……+(i+1-2+1)+……+(n-2+1)= n*(n-1)/2= O(n^2)

總的移動次數(shù)=2+3+……+(2+i-1)+ …… + n = (n+2)*(n-1)/2=O(n^2)

當(dāng)然還要考慮空間復(fù)雜度:其實這里使用了一個變量的存儲空間作為移動數(shù)據(jù)的臨時空間


這里在移動的過程中,可以減少代碼理解的復(fù)雜度,但會在每一個數(shù)據(jù)比較的過程中增加一次比較的次數(shù),如下代碼:

復(fù)制代碼 代碼如下:

 ...
 if (L->elem[i] < L->elem[i-1])  /*第二個數(shù)據(jù)比第一個數(shù)據(jù)小*/
 {
  nCompKey = L->elem[i];
  /*move elements back, compare count add once*/
  for (j = i-1; j >= 0 && L->elem[j] > nCompKey; --j) /*從較大的數(shù)往較小的數(shù)的方向*/
  {
   L->elem[j+1] = L->elem[j];
  }/*在>=退出當(dāng)前循環(huán)*/
  L->elem[j+1] = nCompKey; /*此時val[j]<nCompKey,說明當(dāng)前插入的位置應(yīng)該在j之后*/
 }
 ...

在插入數(shù)據(jù)的過程中,其實前面的數(shù)據(jù)都已經(jīng)排序好了,這時候一個個的進行查找,必定查找次數(shù)較多,如果采用折半查找算法可以減少次數(shù),如下

復(fù)制代碼 代碼如下:

/*折半插入排序算法*/
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;

  /*當(dāng)low=high時,此時不能判斷插入的位置是在low=high的
   *前面還是后面,會進入下面的判斷*/
  while(low <= high)
  {
   mid = (low + high)/2;
   if ( nCompKey > L->elem[mid] )
   {
    low = mid + 1;/*當(dāng)(low=mid+1)>high的時候,跳出循環(huán)*/
   }
   else
   {
    high = mid -1;/*當(dāng)(high=mid-1)<low的時候,跳出循環(huán)*/
   }
  }/*low>high的時候,退出循環(huán)*/

  /*移動nCompKey之前的所有數(shù)據(jù),這里使用high+1是因為high<low
   *的時候,按理數(shù)據(jù)應(yīng)該放在high的位置,但是此時high的位置可
   *能已經(jīng)有排列好的數(shù)據(jù)或者不存在的位置,所以移動為后一個位置*/
  for (j = i-1; j >= high+1; j-- ) /*high是否可以使用low代替??*/
  {
   L->elem[j+1] = L->elem[j];
  }
  /*當(dāng)沒有元素的時候 high=-1*/
  L->elem[high+1] = nCompKey;
 }
 return 0;
}

具體什么原因,請看上面的注釋,這里為什么用high+1,但是此時high與low的位置只相差一個位置,才會跳出while循環(huán),請看下面的改進

復(fù)制代碼 代碼如下:

#if 0
  for (j = i-1; j >= high+1; j-- ) /*high或許可以使用low代替*/
  {
   L->elem[j+1] = L->elem[j];
  }
  L->elem[high+1] = nCompKey;
#else
  for (j = i-1; j >= low; j-- ) /*使用low代替high+1*/
  {
   L->elem[j+1] = L->elem[j];
  }
  L->elem[low] = nCompKey;
#endif
...

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 凌海市| 新闻| 瑞金市| 永平县| 灵川县| 海淀区| 宾阳县| 梁山县| 京山县| 巍山| 资兴市| 荥经县| 渝北区| 河池市| 景宁| 湘潭市| 紫阳县| 辉县市| 察隅县| 大港区| 红桥区| 鄂温| 弋阳县| 莱芜市| 彰化县| 洛浦县| 邛崃市| 阿城市| 沙河市| 咸阳市| 舞钢市| 佛学| 株洲市| 垣曲县| 陆河县| 民乐县| 吉隆县| 会宁县| 年辖:市辖区| 资溪县| 永胜县|