資料結構 C語言版 折半插入排序

2021-09-25 20:58:18 字數 4606 閱讀 7227

折半插入排序是對直接插入排序的優化,折半插入排序所需附加儲存空間和直接插入排序相同,從時間上比較,折半插入排序僅減少了關鍵字間的比較次數,而記錄的移動次數不變。因此,折半插入排序的時間複雜度仍為o(n2)。

直接插入排序可參考:

此處以資料2的排序為例,用i從左到右遍歷到下標為5的位置,發現此處的值2小於前一位的值5下標0

1234

56資料1

3452

6遍歷位置

i將2放到快取0的位置,下限low從下標1開始,上限high從下標i-1開始

計算折半位置為m = (low + high) / 2=2下標0

1234

56資料2

1345

26遍歷位置

lowm

high

i比較r[0]與r[m],發現r[0]下標01

2345

6資料21

3452

6遍歷位置

low,high

m,high+1

i-1i

此時下限與上限相等了,r[high+1]~r[i-1]的記錄後移,即上表資料3,4,5向後移動,移動結果見下表下標0

1234

56資料2

1334

56遍歷位置

low,high

m,high+1

i然後將a[0]插入到high+1的位置,資料2的排序過程結束下標0

1234

56資料2

1234

56遍歷位置

low,high

m,high+1

i說明:本部落格的**實現貼近資料結構(c語言版) 課本**風格,使用抽象資料型別。

專案結構

//真#define false 0

//假#define yes 1

//是#define no 0

//否

#define ok 1

//通過

#define error 0

//錯誤

#define success 1

//成功

#define infeasible -1

//不可行

//#define overflow -2 //堆疊上溢

//#define underflow -3 //堆疊下溢

#define pause system("pause")

//暫停

typedef

int status;

//函式型別,其值為狀態碼

typedef

int datatype;

//抽象資料型別

datatype max

(int num1,..

.);//求一組數中的最大值

datatype min

(int num1,..

.);//求一組數中的最小值

ststus.cpp

#include

"status.h"

/************************************

* 函式名稱: max

* 函式說明:求一組數最大值

* 編寫人員:zipay yu

* 編寫日期:2019/08/02

* 返回型別: datatype 輸入資料個數

* 函式引數: int n 輸入資料個數

* 函式引數: ...

*************************************/

datatype max

(int n,..

.)va_end

(arg)

;return maxnumber;

}/************************************

* 函式名稱: min

* 函式說明:求一組數最小值

* 編寫人員:zipay yu

* 編寫日期:2019/08/02

* 返回型別: datatype

* 函式引數: int n 輸入資料個數

* 函式引數: ...

*************************************/

datatype min

(int n,..

.)va_end

(arg)

;return minnumber;

}

sequencelisttype.h
#pragma once

#include

"status.h"

#define maxsize 20

//順序表最大長度

typedef

int keytype;

//定義關鍵字型別為int

typedef

int infotype;

//定義其他資料項為int

typedef

struct

redtype;

//記錄型別

typedef

struct

sqlist;

//順序表型別

#define lt(a,b) ((a)

#define lq(a,b) ((a)<=(b))

sequencelisttype.cpp
#include

"sequencelisttype.h"

#pragma warning(disable : 4996)

/************************************

* 函式名稱: createsortlist

* 函式說明: 讀取檔案初始化順序表

* 編寫人員: zipay yu

* 編寫日期: 2019/08/02

* 返回型別: status

* 函式引數: sqlist & l

*************************************/

status createsortlist

(sqlist &l)

fscanf

(fpread,

"%d"

,&l.length)

;//讀取資料個數

if(l.length > maxsize)

return error;

for(

int i =

1; i <= l.length; i++

)fclose

(fpread)

;return ok;

}/************************************

* 函式名稱: print

* 函式說明: 列印輸出結構體

* 編寫人員: zipay yu

* 編寫日期: 2019/08/02

* 返回型別: void

* 函式引數: sqlist & l

*************************************/

void

print

(sqlist &l)

printf

("]\n");

}

折半插入排序的實現
/************************************

* 函式名稱: binsertsort

* 函式說明: 對順序表l作折半插入排序,實現順序表從小到大排序

* 編寫人員: zipay yu

* 編寫日期: 2019/08/04

* 返回型別: void

* 函式引數: sqlist & l

*************************************/

void

binsertsort

(sqlist &l)

else

}for

(int j = i -

1; j >=high +1;

--j)

l.r[high +1]

= l.r[0]

;//插入

}}

data.txt

第乙個數表示順序表資料個數,後面的資料為實際的待排序資料

8 49 38 65 97 76 13 27 49
測試
int

資料結構(c語言版)排序1 插入排序

排序的概念 將乙個無序的序列排列成乙個有序的序列 按儲存介質可乙個分為 1.內部排序 資料量不大,資料在記憶體,無需內外存交換資料。2.外部排序 資料量較大,資料在外存,檔案排序 按比較器個數可分為 1.序列排序 單處理機 同時比較一對元素 2.並行排序 多處理機 同時比較多對元素 按主要操作可分為...

資料結構與演算法(C語言版) 插入排序

低階排序演算法 氣泡排序,選擇排序,插入排序 高階排序演算法 歸併排序,堆排序,快速排序 插入排序就是在已經排序的資料中從大往小比較,出現比該數小的就插入到該位置後面。include using namespace std void insertionsort int a,int n int mai...

插入排序(c語言版)

插入排序 insertion sort 的基本思想 每次將乙個待排序的記錄,按其關鍵字大小插入到前面已經排序好的序列中,直到全部記錄插入完成為止.假設待排序的記錄存放在陣列r 1.n 中。初始時,r 1 自成1個有序區,無序區為r 2.n 從i 2起直到i n 為止,依次將r i 插入當前的有序區r...