Data Structure
  • 資料結構自學筆記
  • 1 - Stack & Queue
    • 1.1 - Stack
    • 1.2 - Queue
    • 1.3 - Stack and Queue
  • 2 - Tree & Binary Tree
    • 2.1 - Tree
    • 2.2 - Binary Tree
    • 2.3 - Binary Tree Traversal
    • 2.4 - Binary Search Tree
    • 2.5 - Heap
    • 2.6 - Thread Binary Tree
    • 2.7 - Tree and Binary Tree Conversion
    • 2.8 Advanced Trees
      • 2.8.1 - Min-Max Heap
      • 2.8.2 - Deap
      • 2.8.3 - Symmetric Min-Max Heap
      • 2.8.4 - Extended Binary Tree
      • 2.8.5 - AVL Tree
      • 2.8.6 - M-Way Search Tree
      • 2.8.7 - B Tree
      • 2.8.8 - Red-Black Tree
      • 2.8.9 - Optimal Binary Search Tree
      • 2.8.10 - Splay Tree
      • 2.8.11 - Leftest Heap
      • 2.8.12 - Binomial Heap
  • 3 - Search & Sort
    • 3.1 - Searching
    • 3.2 - Elementary Sorting
      • 3.2.1 - Insertion Sort
      • 3.2.2 - Selection Sort
      • 3.2.3 - Bubble Sort
      • 3.2.4 - Shell Sort
    • 3.3 - Sophisticated Sorting
      • 3.3.1 - Quick Sort
      • 3.3.2 - Merge Sort
      • 3.3.3 - Heap Sort
      • 3.3.4 - Radix Sort
      • 3.3.5 - Bucket Sort
      • 3.3.6 - Counting Sort
    • 3.4 - Summary
  • 4 - Graph
    • 4.1 - Intro
    • 4.2 - Graph Traversal
    • 4.3 - Spanning Tree
      • 4.3.1 - Kruskal's algorithm
      • 4.3.2 - Prim's algorithm
      • 4.3.3 - Sollin's algorithm
    • 4.4 - Shortest Path Length
      • 4.4.1 - Dijkstra's algorithm
      • 4.4.2 - Bellman-Ford algorithm
      • 4.4.3 - Floyd-Warshall algorithm
    • 4.5 - AOV Network
    • 4.6 - AOE Network
    • 4.7 - Others
Powered by GitBook
On this page
  • 1. 演算法
  • 2. 性質
  • 3. 其他
  1. 3 - Search & Sort
  2. 3.2 - Elementary Sorting

3.2.1 - Insertion Sort

Previous3.2 - Elementary SortingNext3.2.2 - Selection Sort

Last updated 6 years ago

1. 演算法

將第iii筆資料插入到前面i−1i-1i−1筆已經排好的資料當中,形成iii筆排好的資料。

void Insert(char *arr, int index){
    int former = index-1;
    int insert = arr[index];
    while (insert < arr[former]){
        arr[former+1] = arr[former];
        former--;
    }
    arr[former+1] = insert;
}

void Sort(char *arr){
    for (int i = 1; i < strlen(arr); i++)
        Insert(arr, i);
}

演算法解析:

input={5,3,8,2,6}input = \{5, 3, 8, 2, 6\}input={5,3,8,2,6}

i=1, Insert(input, 1), insert=3while(3<5)⇒5, 5‾, 8, 2, 6input[0]=insert⇒ 3, 5‾, 8, 2, 6i = 1,\ Insert(input,\ 1),\ insert=3\\ while(3<5)\Rightarrow\underline{5,\ 5},\ 8,\ 2,\ 6\\ input[0]=insert\Rightarrow\ \underline{3,\ 5},\ 8,\ 2,\ 6i=1, Insert(input, 1), insert=3while(3<5)⇒5, 5​, 8, 2, 6input[0]=insert⇒ 3, 5​, 8, 2, 6

2. 性質

  • Time Complexity: Best Case O(n)

  • Space Complexity: O(1)

Insertion sorting is a stable sorting method.

3. 其他

Insertion sort主要分成兩個步驟,

  1. 找到正確的插入位置(linear search)⇒O(n)

所以可以朝這兩個步驟來改善時間效率。

3.1 Binary Insertion Sort

  1. 使用Binary Search找到正確位置⇒O(logn)

3.2 Linear Insertion Sort

  1. 找到正確的插入位置(linear search)⇒O(n)

  2. 改用Linklist來挪動資料⇒O(1)

i=2, Insert(input, 2), insert=8while(8<5)⇒×input[2]=insert⇒ 3, 5, 8‾, 2, 6i = 2,\ Insert(input,\ 2),\ insert=8\\ while(8<5)\Rightarrow \times \\ input[2]=insert\Rightarrow\ 3,\ 5,\ \underline{8},\ 2,\ 6i=2, Insert(input, 2), insert=8while(8<5)⇒×input[2]=insert⇒ 3, 5, 8​, 2, 6

i=3, Insert(input, 3), insert=2while(2<8)⇒3, 5, 8, 8‾, 6,former=1while(2<5)⇒3, 5, 5‾, 8, 6,former=0while(2<3)⇒3, 3‾, 5, 8, 6,former=−1input[0]=insert⇒ 2, 3, 5, 8, 6i = 3,\ Insert(input,\ 3),\ insert=2\\ while(2<8)\Rightarrow 3,\ 5,\ 8,\ \underline{8},\ 6\quad ,former =1\\ while(2<5)\Rightarrow 3,\ 5,\ \underline{5},\ 8,\ 6\quad ,former =0\\ \\ while(2<3)\Rightarrow 3,\ \underline{3},\ 5,\ 8,\ 6\quad ,former =-1\\ \\ input[0]=insert\Rightarrow\ 2,\ 3,\ 5,\ 8,\ 6i=3, Insert(input, 3), insert=2while(2<8)⇒3, 5, 8, 8​, 6,former=1while(2<5)⇒3, 5, 5​, 8, 6,former=0while(2<3)⇒3, 3​, 5, 8, 6,former=−1input[0]=insert⇒ 2, 3, 5, 8, 6

i=4, Insert(input, 4), insert=6while(6<8)⇒2, 3, 5, 8, 8‾,former=2while(6<5)⇒×input[3]=insert⇒ 2, 3, 5, 8, 6i = 4,\ Insert(input,\ 4),\ insert=6\\ while(6<8)\Rightarrow 2,\ 3,\ 5,\ 8,\ \underline{8}\quad ,former =2\\ while(6<5)\Rightarrow\times \\ input[3]=insert\Rightarrow\ 2,\ 3,\ 5,\ 8,\ 6i=4, Insert(input, 4), insert=6while(6<8)⇒2, 3, 5, 8, 8​,former=2while(6<5)⇒×input[3]=insert⇒ 2, 3, 5, 8, 6

將 n−in-in−i筆資料往後挪動⇒O(n)

將 n−in-in−i筆資料往後挪動⇒O(n)