學習了插入排序

2022-09-03 18:45:12 字數 1138 閱讀 8685

簡單的理解,插入排序,就是原佇列中不斷的出列乙個值,與已經出列的所有值進行一一比較,找到自己的位置進行插隊。

下面是學習的來的插入排序以及自己對一些**的注釋;另外,在此基礎上將其中的插隊**,單獨做成了乙個函式。

下面是插入排序的c++**:

1 #include 2

3using

namespace

std;

45 template

6void insertionsort(t *p, int

len);78

intmain()9;

11 insertionsort(q, 10

);12

for(int i=0; i<10; i++)

1316 cout <17return0;

18}1920 template

21void insertionsort(t *p, int

len)

2235 p[line_up] = tem;//

索引位置選好後就可以插隊了36}

37 }

改動的c++**

1 #include 2

3using

namespace

std;

45 template

6void insertionsort(t *p, int

len);

78 template

9void insert(const t& m, t *b, int

j);10

11int

main()12;

14 insertionsort(q, 10

);15

for(int i=0; i<10; i++)

1619 cout <20return0;

21}2223 template

24void insertionsort(t *p, int

len)

2532}33

34 template

35void insert(const t& m, t *b, int

j)36

43 b[i] =m;

44 }

插入排序 學習

直接插入排序 insertion sort 的基本思想是 每次將乙個待排序的記錄,按其關鍵字大小插入到前面已經排好序的子串行中的適當位置,直到全部記錄插入完成為止。二舉例有序 無序 第一趟 27 65 59 64 58 第二趟 27 59 65 64 58 第三趟 27 59 64 65 58 第四...

插入排序 折半插入排序

折半插入排序 binary insertion sort 直接插入排序採用順序查詢法查詢當前記錄在已排好序的序列中插入位置,這個 查詢 操作可利用 折半查詢 來實 現,由此進行的插入排序稱之為折半插入排序 binary insertion sort 演算法思想 1 將待排序的記錄存放在陣列r 1.n...

插入排序 折半插入排序

折半插入排序是基於直接插入排序的優化。直接插入排序 將第i個元素插入時,通過折半查詢的方式,來查詢第i個元素合適的位置。當0 i 1 位置上的元素都已經排序ok,現需要插入第i個元素,設其值為temp 令low 0,high i,mid high low 2。那麼temp可能插入的位置是 low h...