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. 性質
  1. 3 - Search & Sort
  2. 3.2 - Elementary Sorting

3.2.3 - Bubble Sort

Previous3.2.2 - Selection SortNext3.2.4 - Shell Sort

Last updated 6 years ago

1. 演算法

n筆資料由左而右依序兩筆資料相互比較,若前者大於後者則前後交換,共做n-1回合。若某一回合中完全無swap發生,則代表sort完成。

void Sort(char *arr){
    int n = strlen(arr);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n-i-1; j++)
            if (arr[j] > arr[j+1]){ //swap
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
    }
}

演算法分析:

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

i=0, j=0 to 3j=0⇒3,5,‾8,2,6j=1⇒×j=2⇒3,5,2,8,‾6j=3⇒3,5,2,6,8‾i=0,\ j=0\ to\ 3\\ j=0 \Rightarrow \underline{3, 5,} 8, 2, 6\\ j=1 \Rightarrow \times \\ j=2 \Rightarrow3,5,\underline{2,8,}6 \\ j=3 \Rightarrow 3,5,2,\underline{6,8} i=0, j=0 to 3j=0⇒3,5,​8,2,6j=1⇒×j=2⇒3,5,2,8,​6j=3⇒3,5,2,6,8​

i=1, j=0 to 2j=0⇒×j=1⇒3,2,5,‾6,8j=2⇒×i=1,\ j=0\ to\ 2\\ j=0 \Rightarrow \times \\ j=1 \Rightarrow 3, \underline{2, 5,} 6, 8\\ j=2 \Rightarrow \timesi=1, j=0 to 2j=0⇒×j=1⇒3,2,5,​6,8j=2⇒×

i=2, j=0 to 1j=0⇒2,3,‾5,6,8j=1⇒×i=2,\ j=0\ to\ 1\\ j=0 \Rightarrow \underline{2, 3,} 5,6, 8\\ j=1 \Rightarrow \times i=2, j=0 to 1j=0⇒2,3,​5,6,8j=1⇒×

2. 性質

  • Time Complexity:

  • Space Complexity: O(1)

Bubble sorting is a stable sorting method.

Best Case: 資料恰好由小到大排列 O(n)O(n)O(n)

Worst Case: 資料恰好由大到小排列 O(n2)O(n^2)O(n2)

比較次數= (n−1)+(n−2)+...+1=n(n−1)2(n−1)+(n−2)+...+1=\frac{n(n−1)}{2}(n−1)+(n−2)+...+1=2n(n−1)​

Avg Case: O(n2)O(n^2)O(n2)