跳至主要內容

冒泡排序

Mr.Heaboy大约 2 分钟

冒泡排序

本页面将简要介绍冒泡排序。

定义

冒泡排序(英语:Bubble sort)是一种简单的排序算法。由于在算法的执行过程中,较小的元素像是气泡般慢慢「浮」到数列的顶端,故叫做冒泡排序。

过程

它的工作原理是每次检查相邻两个元素,如果前面的元素与后面的元素满足给定的排序条件,就将相邻两个元素交换。当没有相邻的元素需要交换时,排序就完成了。

经过 ii 次扫描后,数列的末尾 ii 项必然是最大的 ii 项,因此冒泡排序最多需要扫描 n1n-1 遍数组就能完成排序。

性质

稳定性

冒泡排序是一种稳定的排序算法。

时间复杂度

在序列完全有序时,冒泡排序只需遍历一遍数组,不用执行任何交换操作,时间复杂度为 O(n)O(n)

在最坏情况下,冒泡排序要执行 (n1)n2\frac{(n-1)n}{2} 次交换操作,时间复杂度为 O(n2)O(n^2)

冒泡排序的平均时间复杂度为 O(n2)O(n^2)

代码实现

伪代码

1Input. An array A consisting of n elements.2Output. A will be sorted in nondecreasing order stably.3Method. 4flagTrue5while flag6flagFalse7for i1 to n18if A[i]>A[i+1]9flagTrue10Swap A[i] and A[i+1] \begin{array}{ll} 1 & \textbf{Input. } \text{An array } A \text{ consisting of }n\text{ elements.} \\ 2 & \textbf{Output. } A\text{ will be sorted in nondecreasing order stably.} \\ 3 & \textbf{Method. } \\ 4 & flag\gets True\\ 5 & \textbf{while }flag\\ 6 & \qquad flag\gets False\\ 7 & \qquad\textbf{for }i\gets1\textbf{ to }n-1\\ 8 & \qquad\qquad\textbf{if }A[i]>A[i + 1]\\ 9 & \qquad\qquad\qquad flag\gets True\\ 10 & \qquad\qquad\qquad \text{Swap } A[i]\text{ and }A[i + 1] \end{array}

C++

    // 假设数组的大小是 n + 1,冒泡排序从数组下标 1 开始
    void bubble_sort(int *a, int n) {
      bool flag = true;
      while (flag) {
        flag = false;
        for (int i = 1; i < n; ++i) {
          if (a[i] > a[i + 1]) {
            flag = true;
            int t = a[i];
            a[i] = a[i + 1];
            a[i + 1] = t;
          }
        }
      }
    }

Python

    def bubble_sort(a, n):
        flag = True
        while flag:
            flag = False
            for i in range(1, n):
                if a[i] > a[i + 1]:
                    flag = True
                    a[i], a[i + 1] = a[i + 1], a[i]

Java

    // 假设数组的大小是 n + 1,冒泡排序从数组下标 1 开始
    static void bubble_sort(int[] a, int n) {
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < n; i++) {
                if (a[i] > a[i + 1]) {
                    flag = true;
                    int t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }
        }
    }