时间复杂度

统计算法运行时间

运行时间可以直观且准确地反映算法的效率。然而,如果我们想要准确预估一段代码的运行时间,应该如何操作呢?

  1. 确定运行平台,包括硬件配置、编程语言、系统环境等,这些因素都会影响代码的运行效率。
  2. 评估各种计算操作所需的运行时间,例如加法操作 + 需要 1 ns,乘法操作 * 需要 10 ns,打印操作需要 5 ns 等。
  3. 统计代码中所有的计算操作,并将所有操作的执行时间求和,从而得到运行时间。

例如以下代码,输入数据大小为 $n$ ,根据以上方法,可以得到算法运行时间为 $6n + 12$ ns 。

$$ 1 + 1 + 10 + (1 + 5) \times n = 6n + 12 $$

Java

// 在某运行平台下
void algorithm(int n) {
    int a = 2;  // 1 ns
    a = a + 1;  // 1 ns
    a = a * 2;  // 10 ns
    // 循环 n 次
    for (int i = 0; i < n; i++) {  // 1 ns ,每轮都要执行 i++
        System.out.println(0);     // 5 ns
    }
}

C++

// 在某运行平台下
void algorithm(int n) {
    int a = 2;  // 1 ns
    a = a + 1;  // 1 ns
    a = a * 2;  // 10 ns
    // 循环 n 次
    for (int i = 0; i < n; i++) {  // 1 ns ,每轮都要执行 i++
        cout << 0 << endl;         // 5 ns
    }
}

Python

# 在某运行平台下
def algorithm(n: int) -> None:
    a = 2      # 1 ns
    a = a + 1  # 1 ns
    a = a * 2  # 10 ns
    # 循环 n 次
    for _ in range(n):  # 1 ns
        print(0)        # 5 ns

Go

// 在某运行平台下
func algorithm(n int) {
    a := 2      // 1 ns
    a = a + 1   // 1 ns
    a = a * 2   // 10 ns
    // 循环 n 次
    for i := 0; i < n; i++ {    // 1 ns
        fmt.Println(a)          // 5 ns
    }
}

JavaScript

// 在某运行平台下
function algorithm(n) {
    var a = 2; // 1 ns
    a = a + 1; // 1 ns
    a = a * 2; // 10 ns
    // 循环 n 次
    for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++
        console.log(0); // 5 ns
    }
}

TypeScript

// 在某运行平台下
function algorithm(n: number): void {
    var a: number = 2; // 1 ns
    a = a + 1; // 1 ns
    a = a * 2; // 10 ns
    // 循环 n 次
    for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++
        console.log(0); // 5 ns
    }
}

C

// 在某运行平台下
void algorithm(int n) {
    int a = 2;  // 1 ns
    a = a + 1;  // 1 ns
    a = a * 2;  // 10 ns
    // 循环 n 次
    for (int i = 0; i < n; i++) {   // 1 ns ,每轮都要执行 i++
        printf("%d", 0);            // 5 ns
    }
}

C#

// 在某运行平台下
void algorithm(int n)
{
    int a = 2;  // 1 ns
    a = a + 1;  // 1 ns
    a = a * 2;  // 10 ns
    // 循环 n 次
    for (int i = 0; i < n; i++)
    {  // 1 ns ,每轮都要执行 i++
        Console.WriteLine(0);     // 5 ns
    }
}

Swift

// 在某运行平台下
func algorithm(n: Int) {
    var a = 2 // 1 ns
    a = a + 1 // 1 ns
    a = a * 2 // 10 ns
    // 循环 n 次
    for _ in 0 ..< n { // 1 ns
        print(0) // 5 ns
    }
}

Zig


然而实际上,统计算法的运行时间既不合理也不现实。首先,我们不希望预估时间和运行平台绑定,因为算法需要在各种不同的平台上运行。其次,我们很难获知每种操作的运行时间,这给预估过程带来了极大的难度。

统计时间增长趋势

「时间复杂度分析」采取了一种不同的方法,其统计的不是算法运行时间,而是算法运行时间随着数据量变大时的增长趋势

“时间增长趋势”这个概念较为抽象,我们通过一个例子来加以理解。假设输入数据大小为 $n$ ,给定三个算法 A , B , C

Java

// 算法 A 时间复杂度:常数阶
void algorithm_A(int n) {
    System.out.println(0);
}
// 算法 B 时间复杂度:线性阶
void algorithm_B(int n) {
    for (int i = 0; i < n; i++) {
        System.out.println(0);
    }
}
// 算法 C 时间复杂度:常数阶
void algorithm_C(int n) {
    for (int i = 0; i < 1000000; i++) {
        System.out.println(0);
    }
}

C++

// 算法 A 时间复杂度:常数阶
void algorithm_A(int n) {
    cout << 0 << endl;
}
// 算法 B 时间复杂度:线性阶
void algorithm_B(int n) {
    for (int i = 0; i < n; i++) {
        cout << 0 << endl;
    }
}
// 算法 C 时间复杂度:常数阶
void algorithm_C(int n) {
    for (int i = 0; i < 1000000; i++) {
        cout << 0 << endl;
    }
}

Python

# 算法 A 时间复杂度:常数阶
def algorithm_A(n: int) -> None:
    print(0)
# 算法 B 时间复杂度:线性阶
def algorithm_B(n: int) -> None:
    for _ in range(n):
        print(0)
# 算法 C 时间复杂度:常数阶
def algorithm_C(n: int) -> None:
    for _ in range(1000000):
        print(0)

Go

// 算法 A 时间复杂度:常数阶
func algorithm_A(n int) {
    fmt.Println(0)
}
// 算法 B 时间复杂度:线性阶
func algorithm_B(n int) {
    for i := 0; i < n; i++ {
        fmt.Println(0)
    }
}
// 算法 C 时间复杂度:常数阶
func algorithm_C(n int) {
    for i := 0; i < 1000000; i++ {
        fmt.Println(0)
    }
}

JavaScript

// 算法 A 时间复杂度:常数阶
function algorithm_A(n) {
    console.log(0);
}
// 算法 B 时间复杂度:线性阶
function algorithm_B(n) {
    for (let i = 0; i < n; i++) {
        console.log(0);
    }
}
// 算法 C 时间复杂度:常数阶
function algorithm_C(n) {
    for (let i = 0; i < 1000000; i++) {
        console.log(0);
    }
}

TypeScript

// 算法 A 时间复杂度:常数阶
function algorithm_A(n: number): void {
    console.log(0);
}
// 算法 B 时间复杂度:线性阶
function algorithm_B(n: number): void {
    for (let i = 0; i < n; i++) {
        console.log(0);
    }
}
// 算法 C 时间复杂度:常数阶
function algorithm_C(n: number): void {
    for (let i = 0; i < 1000000; i++) {
        console.log(0);
    }
}

C

// 算法 A 时间复杂度:常数阶
void algorithm_A(int n) {
    printf("%d", 0);
}
// 算法 B 时间复杂度:线性阶
void algorithm_B(int n) {
    for (int i = 0; i < n; i++) {
        printf("%d", 0);
    }
}
// 算法 C 时间复杂度:常数阶
void algorithm_C(int n) {
    for (int i = 0; i < 1000000; i++) {
        printf("%d", 0);
    }
}

C#

// 算法 A 时间复杂度:常数阶
void algorithm_A(int n)
{
    Console.WriteLine(0);
}
// 算法 B 时间复杂度:线性阶
void algorithm_B(int n)
{
    for (int i = 0; i < n; i++)
    {
        Console.WriteLine(0);
    }
}
// 算法 C 时间复杂度:常数阶
void algorithm_C(int n)
{
    for (int i = 0; i < 1000000; i++)
    {
        Console.WriteLine(0);
    }
}

Swift

// 算法 A 时间复杂度:常数阶
func algorithmA(n: Int) {
    print(0)
}

// 算法 B 时间复杂度:线性阶
func algorithmB(n: Int) {
    for _ in 0 ..< n {
        print(0)
    }
}

// 算法 C 时间复杂度:常数阶
func algorithmC(n: Int) {
    for _ in 0 ..< 1000000 {
        print(0)
    }
}

Zig


算法 A, B, C 的时间增长趋势

相较于直接统计算法运行时间,时间复杂度分析有哪些优势和局限性呢?

时间复杂度能够有效评估算法效率。例如,算法 B 的运行时间呈线性增长,在 $n > 1$ 时比算法 A 慢,在 $n > 1000000$ 时比算法 C 慢。事实上,只要输入数据大小 $n$ 足够大,复杂度为「常数阶」的算法一定优于「线性阶」的算法,这正是时间增长趋势所表达的含义。

时间复杂度的推算方法更简便。显然,运行平台和计算操作类型都与算法运行时间的增长趋势无关。因此在时间复杂度分析中,我们可以简单地将所有计算操作的执行时间视为相同的“单位时间”,从而将“计算操作的运行时间的统计”简化为“计算操作的数量的统计”,这样的简化方法大大降低了估算难度。

时间复杂度也存在一定的局限性。例如,尽管算法 AC 的时间复杂度相同,但实际运行时间差别很大。同样,尽管算法 B 的时间复杂度比 C 高,但在输入数据大小 $n$ 较小时,算法 B 明显优于算法 C 。在这些情况下,我们很难仅凭时间复杂度判断算法效率高低。当然,尽管存在上述问题,复杂度分析仍然是评判算法效率最有效且常用的方法。

函数渐近上界

设算法的计算操作数量是一个关于输入数据大小 $n$ 的函数,记为 $T(n)$ ,则以下算法的操作数量为

$$ T(n) = 3 + 2n $$

Java

void algorithm(int n) {
    int a = 1;  // +1
    a = a + 1;  // +1
    a = a * 2;  // +1
    // 循环 n 次
    for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
        System.out.println(0);    // +1
    }
}

C++

void algorithm(int n) {
    int a = 1;  // +1
    a = a + 1;  // +1
    a = a * 2;  // +1
    // 循环 n 次
    for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
        cout << 0 << endl;    // +1
    }
}

Python

def algorithm(n: int) -> None:
    a: int = 1  # +1
    a = a + 1  # +1
    a = a * 2  # +1
    # 循环 n 次
    for i in range(n):  # +1
        print(0)        # +1

Go

func algorithm(n int) {
    a := 1      // +1
    a = a + 1   // +1
    a = a * 2   // +1
    // 循环 n 次
    for i := 0; i < n; i++ {   // +1
        fmt.Println(a)         // +1
    }
}

JavaScript

function algorithm(n) {
    var a = 1; // +1
    a += 1; // +1
    a *= 2; // +1
    // 循环 n 次
    for(let i = 0; i < n; i++){ // +1(每轮都执行 i ++)
        console.log(0); // +1
    }
}

TypeScript

function algorithm(n: number): void{
    var a: number = 1; // +1
    a += 1; // +1
    a *= 2; // +1
    // 循环 n 次
    for(let i = 0; i < n; i++){ // +1(每轮都执行 i ++)
        console.log(0); // +1
    }
}

C

void algorithm(int n) {
    int a = 1;  // +1
    a = a + 1;  // +1
    a = a * 2;  // +1
    // 循环 n 次
    for (int i = 0; i < n; i++) {   // +1(每轮都执行 i ++)
        printf("%d", 0);            // +1
    }
}  

C#

void algorithm(int n)
{
    int a = 1;  // +1
    a = a + 1;  // +1
    a = a * 2;  // +1
    // 循环 n 次
    for (int i = 0; i < n; i++) // +1(每轮都执行 i ++)
    {
        Console.WriteLine(0);   // +1
    }
}

Swift

func algorithm(n: Int) {
    var a = 1 // +1
    a = a + 1 // +1
    a = a * 2 // +1
    // 循环 n 次
    for _ in 0 ..< n { // +1
        print(0) // +1
    }
}

Zig


$T(n)$ 是一次函数,说明时间增长趋势是线性的,因此可以得出时间复杂度是线性阶。

我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号称为「大 $O$ 记号 Big-$O$ Notation」,表示函数 $T(n)$ 的「渐近上界 Asymptotic Upper Bound」。

推算时间复杂度本质上是计算“操作数量函数 $T(n)$”的渐近上界。接下来,我们来看函数渐近上界的数学定义。

摘要:函数渐近上界

若存在正实数 $c$ 和实数 $n_0$ ,使得对于所有的 $n > n_0$ ,均有 $$ T(n) \leq c \cdot f(n) $$ 则可认为 $f(n)$ 给出了 $T(n)$ 的一个渐近上界,记为 $$ T(n) = O(f(n)) $$

函数的渐近上界

从本质上讲,计算渐近上界就是寻找一个函数 $f(n)$ ,使得当 $n$ 趋向于无穷大时,$T(n)$ 和 $f(n)$ 处于相同的增长级别,仅相差一个常数项 $c$ 的倍数。

推算方法

渐近上界的数学味儿有点重,如果你感觉没有完全理解,也无需担心。因为在实际使用中,我们只需要掌握推算方法,数学意义可以逐渐领悟。

根据定义,确定 $f(n)$ 之后,我们便可得到时间复杂度 $O(f(n))$ 。那么如何确定渐近上界 $f(n)$ 呢?总体分为两步:首先统计操作数量,然后判断渐近上界。

1) 统计操作数量

针对代码,逐行从上到下计算即可。然而,由于上述 $c \cdot f(n)$ 中的常数项 $c$ 可以取任意大小,因此操作数量 $T(n)$ 中的各种系数、常数项都可以被忽略。根据此原则,可以总结出以下计数简化技巧:

  1. 忽略与 $n$ 无关的操作。因为它们都是 $T(n)$ 中的常数项,对时间复杂度不产生影响。
  2. 省略所有系数。例如,循环 $2n$ 次、$5n + 1$ 次等,都可以简化记为 $n$ 次,因为 $n$ 前面的系数对时间复杂度没有影响。
  3. 循环嵌套时使用乘法。总操作数量等于外层循环和内层循环操作数量之积,每一层循环依然可以分别套用上述 1.2. 技巧。

以下示例展示了使用上述技巧前、后的统计结果。

$$ \begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \text{完整统计 (-.-|||)} \newline & = 2n^2 + 7n + 3 \newline T(n) & = n^2 + n & \text{偷懒统计 (o.O)} \end{aligned} $$

最终,两者都能推出相同的时间复杂度结果,即 $O(n^2)$ 。

Java

void algorithm(int n) {
    int a = 1;  // +0(技巧 1)
    a = a + n;  // +0(技巧 1)
    // +n(技巧 2)
    for (int i = 0; i < 5 * n + 1; i++) {
        System.out.println(0);
    }
    // +n*n(技巧 3)
    for (int i = 0; i < 2 * n; i++) {
        for (int j = 0; j < n + 1; j++) {
            System.out.println(0);
        }
    }
}

C++

void algorithm(int n) {
    int a = 1;  // +0(技巧 1)
    a = a + n;  // +0(技巧 1)
    // +n(技巧 2)
    for (int i = 0; i < 5 * n + 1; i++) {
        cout << 0 << endl;
    }
    // +n*n(技巧 3)
    for (int i = 0; i < 2 * n; i++) {
        for (int j = 0; j < n + 1; j++) {
            cout << 0 << endl;
        }
    }
}

Python

def algorithm(n: int) -> None:
    a: int = 1  # +0(技巧 1)
    a = a + n   # +0(技巧 1)
    # +n(技巧 2)
    for i in range(5 * n + 1):
        print(0)
    # +n*n(技巧 3)
    for i in range(2 * n):
        for j in range(n + 1):
            print(0)

Go

func algorithm(n int) {
    a := 1      // +0(技巧 1)
    a = a + n  // +0(技巧 1)
    // +n(技巧 2)
    for i := 0; i < 5 * n + 1; i++ {
        fmt.Println(0)
    }
    // +n*n(技巧 3)
    for i := 0; i < 2 * n; i++ {
        for j := 0; j < n + 1; j++ {
            fmt.Println(0)
        }
    }
}

JavaScript

function algorithm(n) {
    let a = 1;  // +0(技巧 1)
    a = a + n;  // +0(技巧 1)
    // +n(技巧 2)
    for (let i = 0; i < 5 * n + 1; i++) {
        console.log(0);
    }
    // +n*n(技巧 3)
    for (let i = 0; i < 2 * n; i++) {
        for (let j = 0; j < n + 1; j++) {
            console.log(0);
        }
    }
}

TypeScript

function algorithm(n: number): void {
    let a = 1;  // +0(技巧 1)
    a = a + n;  // +0(技巧 1)
    // +n(技巧 2)
    for (let i = 0; i < 5 * n + 1; i++) {
        console.log(0);
    }
    // +n*n(技巧 3)
    for (let i = 0; i < 2 * n; i++) {
        for (let j = 0; j < n + 1; j++) {
            console.log(0);
        }
    }
}

C

void algorithm(int n) {
    int a = 1;  // +0(技巧 1)
    a = a + n;  // +0(技巧 1)
    // +n(技巧 2)
    for (int i = 0; i < 5 * n + 1; i++) {
        printf("%d", 0);
    }
    // +n*n(技巧 3)
    for (int i = 0; i < 2 * n; i++) {
        for (int j = 0; j < n + 1; j++) {
            printf("%d", 0);
        }
    }
}

C#

void algorithm(int n)
{
    int a = 1;  // +0(技巧 1)
    a = a + n;  // +0(技巧 1)
    // +n(技巧 2)
    for (int i = 0; i < 5 * n + 1; i++)
    {
        Console.WriteLine(0);
    }
    // +n*n(技巧 3)
    for (int i = 0; i < 2 * n; i++)
    {
        for (int j = 0; j < n + 1; j++)
        {
            Console.WriteLine(0);
        }
    }
}

Swift

func algorithm(n: Int) {
    var a = 1 // +0(技巧 1)
    a = a + n // +0(技巧 1)
    // +n(技巧 2)
    for _ in 0 ..< (5 * n + 1) {
        print(0)
    }
    // +n*n(技巧 3)
    for _ in 0 ..< (2 * n) {
        for _ in 0 ..< (n + 1) {
            print(0)
        }
    }
}

Zig


2) 判断渐近上界

时间复杂度由多项式 $T(n)$ 中最高阶的项来决定。这是因为在 $n$ 趋于无穷大时,最高阶的项将发挥主导作用,其他项的影响都可以被忽略。

以下表格展示了一些例子,其中一些夸张的值是为了强调“系数无法撼动阶数”这一结论。当 $n$ 趋于无穷大时,这些常数变得无足轻重。

操作数量 $T(n)$ 时间复杂度 $O(f(n))$
$100000$ $O(1)$
$3n + 2$ $O(n)$
$2n^2 + 3n + 2$ $O(n^2)$
$n^3 + 10000n^2$ $O(n^3)$
$2^n + 10000n^{10000}$ $O(2^n)$

常见类型

设输入数据大小为 $n$ ,常见的时间复杂度类型包括(按照从低到高的顺序排列):

$$ \begin{aligned} O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!) \newline \text{常数阶} < \text{对数阶} < \text{线性阶} < \text{线性对数阶} < \text{平方阶} < \text{指数阶} < \text{阶乘阶} \end{aligned} $$

时间复杂度的常见类型

提示

部分示例代码需要一些预备知识,包括数组、递归算法等。如果遇到不理解的部分,请不要担心,可以在学习完后面章节后再回顾。现阶段,请先专注于理解时间复杂度的含义和推算方法。

常数阶 $O(1)$

常数阶的操作数量与输入数据大小 $n$ 无关,即不随着 $n$ 的变化而变化。

对于以下算法,尽管操作数量 size 可能很大,但由于其与数据大小 $n$ 无关,因此时间复杂度仍为 $O(1)$ 。

Java

/* 常数阶 */
int constant(int n) {
    int count = 0;
    int size = 100000;
    for (int i = 0; i < size; i++)
        count++;
    return count;
}

C++

/* 常数阶 */
int constant(int n) {
    int count = 0;
    int size = 100000;
    for (int i = 0; i < size; i++)
        count++;
    return count;
}

Python

def constant(n: int) -> int:
    """常数阶"""
    count: int = 0
    size: int = 100000
    for _ in range(size):
        count += 1
    return count

Go

/* 常数阶 */
func constant(n int) int {
    count := 0
    size := 100000
    for i := 0; i < size; i++ {
        count++
    }
    return count
}

JavaScript

function constant(n) {
    let count = 0;
    const size = 100000;
    for (let i = 0; i < size; i++) count++;
    return count;
}

TypeScript

function constant(n: number): number {
    let count = 0;
    const size = 100000;
    for (let i = 0; i < size; i++) count++;
    return count;
}

C

int constant(int n) {
    int count = 0;
    int size = 100000;
    int i = 0;
    for (int i = 0; i < size; i++) {
        count++;
    }
    return count;
}

C#

int constant(int n)
{
    int count = 0;
    int size = 100000;
    for (int i = 0; i < size; i++)
        count++;
    return count;
}

Swift

func constant(n: Int) -> Int {
    var count = 0
    let size = 100_000
    for _ in 0 ..< size {
        count += 1
    }
    return count
}

Zig

fn constant(n: i32) i32 {
    _ = n;
    var count: i32 = 0;
    const size: i32 = 100_000;
    var i: i32 = 0;
    while(i<size) : (i += 1) {
        count += 1;
    }
    return count;
}

线性阶 $O(n)$

线性阶的操作数量相对于输入数据大小以线性级别增长。线性阶通常出现在单层循环中。

Java

/* 线性阶 */
int linear(int n) {
    int count = 0;
    for (int i = 0; i < n; i++)
        count++;
    return count;
}

C++

/* 线性阶 */
int linear(int n) {
    int count = 0;
    for (int i = 0; i < n; i++)
        count++;
    return count;
}

Python

def linear(n: int) -> int:
    """线性阶"""
    count: int = 0
    for _ in range(n):
        count += 1
    return count

Go

/* 线性阶 */
func linear(n int) int {
    count := 0
    for i := 0; i < n; i++ {
        count++
    }
    return count
}

JavaScript

/* 线性阶 */
function linear(n) {
    let count = 0;
    for (let i = 0; i < n; i++) count++;
    return count;
}

TypeScript

/* 线性阶 */
function linear(n: number): number {
    let count = 0;
    for (let i = 0; i < n; i++) count++;
    return count;
}

C

/* 线性阶 */
int linear(int n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        count++;
    }
    return count;
}

C#

/* 线性阶 */
int linear(int n)
{
    int count = 0;
    for (int i = 0; i < n; i++)
        count++;
    return count;
}

Swift

/* 线性阶 */
func linear(n: Int) -> Int {
    var count = 0
    for _ in 0 ..< n {
        count += 1
    }
    return count
}

Zig

// 线性阶
fn linear(n: i32) i32 {
    var count: i32 = 0;
    var i: i32 = 0;
    while (i < n) : (i += 1) {
        count += 1;
    }
    return count;
}

遍历数组和遍历链表等操作的时间复杂度均为 $O(n)$ ,其中 $n$ 为数组或链表的长度。

问题:如何确定输入数据大小 $n$ ?

数据大小 $n$ 需根据输入数据的类型来具体确定。例如,在上述示例中,我们直接将 $n$ 视为输入数据大小;在下面遍历数组的示例中,数据大小 $n$ 为数组的长度。

Java

/* 线性阶(遍历数组) */
int arrayTraversal(int[] nums) {
    int count = 0;
    // 循环次数与数组长度成正比
    for (int num : nums) {
        count++;
    }
    return count;
}

C++

/* 线性阶(遍历数组) */
int arrayTraversal(vector<int> &nums) {
    int count = 0;
    // 循环次数与数组长度成正比
    for (int num : nums) {
        count++;
    }
    return count;
}

Python

def array_traversal(nums: list[int]) -> int:
    """线性阶(遍历数组)"""
    count: int = 0
    # 循环次数与数组长度成正比
    for num in nums:
        count += 1
    return count

Go

/* 线性阶(遍历数组) */
func arrayTraversal(nums []int) int {
    count := 0
    // 循环次数与数组长度成正比
    for range nums {
        count++
    }
    return count
}

JavaScript

/* 线性阶(遍历数组) */
function arrayTraversal(nums) {
    let count = 0;
    // 循环次数与数组长度成正比
    for (let i = 0; i < nums.length; i++) {
        count++;
    }
    return count;
}

TypeScript

/* 线性阶(遍历数组) */
function arrayTraversal(nums: number[]): number {
    let count = 0;
    // 循环次数与数组长度成正比
    for (let i = 0; i < nums.length; i++) {
        count++;
    }
    return count;
}

C

/* 线性阶(遍历数组) */
int arrayTraversal(int *nums, int n) {
    int count = 0;
    // 循环次数与数组长度成正比
    for (int i = 0; i < n; i++) {
        count++;
    }
    return count;
}

C#

/* 线性阶(遍历数组) */
int arrayTraversal(int[] nums)
{
    int count = 0;
    // 循环次数与数组长度成正比
    foreach (int num in nums)
    {
        count++;
    }
    return count;
}

Swift

/* 线性阶(遍历数组) */
func arrayTraversal(nums: [Int]) -> Int {
    var count = 0
    // 循环次数与数组长度成正比
    for _ in nums {
        count += 1
    }
    return count
}

Zig

// 线性阶(遍历数组)
fn arrayTraversal(nums: []i32) i32 {
    var count: i32 = 0;
    // 循环次数与数组长度成正比
    for (nums) |_| {
        count += 1;
    }
    return count;
}

平方阶 $O(n^2)$

平方阶的操作数量相对于输入数据大小以平方级别增长。平方阶通常出现在嵌套循环中,外层循环和内层循环都为 $O(n)$ ,因此总体为 $O(n^2)$ 。

Java

/* 平方阶 */
int quadratic(int n) {
    int count = 0;
    // 循环次数与数组长度成平方关系
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            count++;
        }
    }
    return count;
}

C++

/* 平方阶 */
int quadratic(int n) {
    int count = 0;
    // 循环次数与数组长度成平方关系
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            count++;
        }
    }
    return count;
}

Python

def quadratic(n: int) -> int:
    """平方阶"""
    count: int = 0
    # 循环次数与数组长度成平方关系
    for i in range(n):
        for j in range(n):
            count += 1
    return count

Go

/* 平方阶 */
func quadratic(n int) int {
    count := 0
    // 循环次数与数组长度成平方关系
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            count++
        }
    }
    return count
}

JavaScript

/* 平方阶 */
function quadratic(n) {
    let count = 0;
    // 循环次数与数组长度成平方关系
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            count++;
        }
    }
    return count;
}

TypeScript

/* 平方阶 */
function quadratic(n: number): number {
    let count = 0;
    // 循环次数与数组长度成平方关系
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            count++;
        }
    }
    return count;
}

C

/* 平方阶 */
int quadratic(int n) {
    int count = 0;
    // 循环次数与数组长度成平方关系
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            count++;
        }
    }
    return count;
}

C#

/* 平方阶 */
int quadratic(int n)
{
    int count = 0;
    // 循环次数与数组长度成平方关系
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            count++;
        }
    }
    return count;
}

Swift

/* 平方阶 */
func quadratic(n: Int) -> Int {
    var count = 0
    // 循环次数与数组长度成平方关系
    for _ in 0 ..< n {
        for _ in 0 ..< n {
            count += 1
        }
    }
    return count
}

Zig

// 平方阶
fn quadratic(n: i32) i32 {
    var count: i32 = 0;
    var i: i32 = 0;
    // 循环次数与数组长度成平方关系
    while (i < n) : (i += 1) {
        var j: i32 = 0;
        while (j < n) : (j += 1) {
            count += 1;
        }
    }
    return count;
}

常数阶、线性阶、平方阶的时间复杂度

以「冒泡排序」为例,外层循环执行 $n - 1$ 次,内层循环执行 $n-1, n-2, \cdots, 2, 1$ 次,平均为 $\frac{n}{2}$ 次,因此时间复杂度为 $O(n^2)$ 。

$$ O((n - 1) \frac{n}{2}) = O(n^2) $$

Java

/* 平方阶(冒泡排序) */
int bubbleSort(int[] nums) {
    int count = 0; // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for (int i = nums.length - 1; i > 0; i--) {
        // 内循环:冒泡操作
        for (int j = 0; j < i; j++) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                int tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3; // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

C++

/* 平方阶(冒泡排序) */
int bubbleSort(vector<int> &nums) {
    int count = 0; // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for (int i = nums.size() - 1; i > 0; i--) {
        // 内循环:冒泡操作
        for (int j = 0; j < i; j++) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                int tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3; // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

Python

def bubble_sort(nums: list[int]) -> int:
    """平方阶(冒泡排序)"""
    count: int = 0  # 计数器
    # 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for i in range(len(nums) - 1, 0, -1):
        # 内循环:冒泡操作
        for j in range(i):
            if nums[j] > nums[j + 1]:
                # 交换 nums[j] 与 nums[j + 1]
                tmp: int = nums[j]
                nums[j] = nums[j + 1]
                nums[j + 1] = tmp
                count += 3  # 元素交换包含 3 个单元操作
    return count

Go

/* 平方阶(冒泡排序) */
func bubbleSort(nums []int) int {
    count := 0 // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for i := len(nums) - 1; i > 0; i-- {
        // 内循环:冒泡操作
        for j := 0; j < i; j++ {
            if nums[j] > nums[j+1] {
                // 交换 nums[j] 与 nums[j + 1]
                tmp := nums[j]
                nums[j] = nums[j+1]
                nums[j+1] = tmp
                count += 3 // 元素交换包含 3 个单元操作
            }
        }
    }
    return count
}

JavaScript

/* 平方阶(冒泡排序) */
function bubbleSort(nums) {
    let count = 0; // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for (let i = nums.length - 1; i > 0; i--) {
        // 内循环:冒泡操作
        for (let j = 0; j < i; j++) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                let tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3; // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

TypeScript

/* 平方阶(冒泡排序) */
function bubbleSort(nums: number[]): number {
    let count = 0; // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for (let i = nums.length - 1; i > 0; i--) {
        // 内循环:冒泡操作
        for (let j = 0; j < i; j++) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                let tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3; // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

C

/* 平方阶(冒泡排序) */
int bubbleSort(int *nums, int n) {
    int count = 0; // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for (int i = n - 1; i > 0; i--) {
        // 内循环:冒泡操作
        for (int j = 0; j < i; j++) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                int tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3; // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

C#

/* 平方阶(冒泡排序) */
int bubbleSort(int[] nums) {
    int count = 0;  // 计数器
                    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for (int i = nums.Length - 1; i > 0; i--) {
        // 内循环:冒泡操作
        for (int j = 0; j < i; j++) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                int tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3;  // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

Swift

/* 平方阶(冒泡排序) */
func bubbleSort(nums: inout [Int]) -> Int {
    var count = 0 // 计数器
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    for i in stride(from: nums.count - 1, to: 0, by: -1) {
        // 内循环:冒泡操作
        for j in 0 ..< i {
            if nums[j] > nums[j + 1] {
                // 交换 nums[j] 与 nums[j + 1]
                let tmp = nums[j]
                nums[j] = nums[j + 1]
                nums[j + 1] = tmp
                count += 3 // 元素交换包含 3 个单元操作
            }
        }
    }
    return count
}

Zig

// 平方阶(冒泡排序)
fn bubbleSort(nums: []i32) i32 {
    var count: i32 = 0;  // 计数器 
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
    var i: i32 = @intCast(i32, nums.len ) - 1;
    while (i > 0) : (i -= 1) {
        var j: usize = 0;
        // 内循环:冒泡操作
        while (j < i) : (j += 1) {
            if (nums[j] > nums[j + 1]) {
                // 交换 nums[j] 与 nums[j + 1]
                var tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
                count += 3;  // 元素交换包含 3 个单元操作
            }
        }
    }
    return count;
}

指数阶 $O(2^n)$

说明

生物学的“细胞分裂”是指数阶增长的典型例子:初始状态为 $1$ 个细胞,分裂一轮后变为 $2$ 个,分裂两轮后变为 $4$ 个,以此类推,分裂 $n$ 轮后有 $2^n$ 个细胞。

指数阶增长非常迅速,在实际应用中通常是不可接受的。若一个问题使用「暴力枚举」求解的时间复杂度为 $O(2^n)$ ,那么通常需要使用「动态规划」或「贪心算法」等方法来解决。

Java

/* 指数阶(循环实现) */
int exponential(int n) {
    int count = 0, base = 1;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < base; j++) {
            count++;
        }
        base *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

C++

/* 指数阶(循环实现) */
int exponential(int n) {
    int count = 0, base = 1;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < base; j++) {
            count++;
        }
        base *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

Python

def exponential(n: int) -> int:
    """指数阶(循环实现)"""
    count: int = 0
    base: int = 1
    # cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for _ in range(n):
        for _ in range(base):
            count += 1
        base *= 2
    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count

Go

/* 指数阶(循环实现)*/
func exponential(n int) int {
    count, base := 0, 1
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for i := 0; i < n; i++ {
        for j := 0; j < base; j++ {
            count++
        }
        base *= 2
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count
}

JavaScript

/* 指数阶(循环实现) */
function exponential(n) {
    let count = 0,
        base = 1;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < base; j++) {
            count++;
        }
        base *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

TypeScript

/* 指数阶(循环实现) */
function exponential(n: number): number {
    let count = 0,
        base = 1;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < base; j++) {
            count++;
        }
        base *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

C

/* 指数阶(循环实现) */
int exponential(int n) {
    int count = 0;
    int bas = 1;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < bas; j++) {
            count++;
        }
        bas *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

C#

/* 指数阶(循环实现) */
int exponential(int n) {
    int count = 0, bas = 1;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < bas; j++) {
            count++;
        }
        bas *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

Swift

/* 指数阶(循环实现) */
func exponential(n: Int) -> Int {
    var count = 0
    var base = 1
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    for _ in 0 ..< n {
        for _ in 0 ..< base {
            count += 1
        }
        base *= 2
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count
}

Zig

// 指数阶(循环实现)
fn exponential(n: i32) i32 {
    var count: i32 = 0;
    var bas: i32 = 1;
    var i: i32 = 0;
    // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
    while (i < n) : (i += 1) {
        var j: i32 = 0;
        while (j < bas) : (j += 1) {
            count += 1;
        }
        bas *= 2;
    }
    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
    return count;
}

指数阶的时间复杂度

在实际算法中,指数阶常出现于递归函数。例如以下代码,不断地一分为二,经过 $n$ 次分裂后停止。

Java

/* 指数阶(递归实现) */
int expRecur(int n) {
    if (n == 1)
        return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

C++

/* 指数阶(递归实现) */
int expRecur(int n) {
    if (n == 1)
        return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

Python

def exp_recur(n: int) -> int:
    """指数阶(递归实现)"""
    if n == 1:
        return 1
    return exp_recur(n - 1) + exp_recur(n - 1) + 1

Go

/* 指数阶(递归实现)*/
func expRecur(n int) int {
    if n == 1 {
        return 1
    }
    return expRecur(n-1) + expRecur(n-1) + 1
}

JavaScript

/* 指数阶(递归实现) */
function expRecur(n) {
    if (n == 1) return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

TypeScript

/* 指数阶(递归实现) */
function expRecur(n: number): number {
    if (n == 1) return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

C

/* 指数阶(递归实现) */
int expRecur(int n) {
    if (n == 1)
        return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

C#

/* 指数阶(递归实现) */
int expRecur(int n) {
    if (n == 1) return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

Swift

/* 指数阶(递归实现) */
func expRecur(n: Int) -> Int {
    if n == 1 {
        return 1
    }
    return expRecur(n: n - 1) + expRecur(n: n - 1) + 1
}

Zig

// 指数阶(递归实现)
fn expRecur(n: i32) i32 {
    if (n == 1) return 1;
    return expRecur(n - 1) + expRecur(n - 1) + 1;
}

对数阶 $O(\log n)$

与指数阶相反,对数阶反映了“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长缓慢,是理想的时间复杂度。

对数阶常出现于「二分查找」和「分治算法」中,体现了“一分为多”和“化繁为简”的算法思想。

设输入数据大小为 $n$ ,由于每轮缩减到一半,因此循环次数是 $\log_2 n$ ,即 $2^n$ 的反函数。

Java

/* 对数阶(循环实现) */
int logarithmic(float n) {
    int count = 0;
    while (n > 1) {
        n = n / 2;
        count++;
    }
    return count;
}

C++

/* 对数阶(循环实现) */
int logarithmic(float n) {
    int count = 0;
    while (n > 1) {
        n = n / 2;
        count++;
    }
    return count;
}

Python

def logarithmic(n: float) -> int:
    """对数阶(循环实现)"""
    count: int = 0
    while n > 1:
        n = n / 2
        count += 1
    return count

Go

/* 对数阶(循环实现)*/
func logarithmic(n float64) int {
    count := 0
    for n > 1 {
        n = n / 2
        count++
    }
    return count
}

JavaScript

/* 对数阶(循环实现) */
function logarithmic(n) {
    let count = 0;
    while (n > 1) {
        n = n / 2;
        count++;
    }
    return count;
}

TypeScript

/* 对数阶(循环实现) */
function logarithmic(n: number): number {
    let count = 0;
    while (n > 1) {
        n = n / 2;
        count++;
    }
    return count;
}

C

/* 对数阶(循环实现) */
int logarithmic(float n) {
    int count = 0;
    while (n > 1) {
        n = n / 2;
        count++;
    }
    return count;
}

C#

/* 对数阶(循环实现) */
int logarithmic(float n) {
    int count = 0;
    while (n > 1) {
        n = n / 2;
        count++;
    }
    return count;
}

Swift

/* 对数阶(循环实现) */
func logarithmic(n: Double) -> Int {
    var count = 0
    var n = n
    while n > 1 {
        n = n / 2
        count += 1
    }
    return count
}

Zig

// 对数阶(循环实现)
fn logarithmic(n: f32) i32 {
    var count: i32 = 0;
    var n_var = n;
    while (n_var > 1)
    {
        n_var = n_var / 2;
        count +=1;
    }
    return count;
}

对数阶的时间复杂度

与指数阶类似,对数阶也常出现于递归函数。以下代码形成了一个高度为 $\log_2 n$ 的递归树。

Java

/* 对数阶(递归实现) */
int logRecur(float n) {
    if (n <= 1)
        return 0;
    return logRecur(n / 2) + 1;
}

C++

/* 对数阶(递归实现) */
int logRecur(float n) {
    if (n <= 1)
        return 0;
    return logRecur(n / 2) + 1;
}

Python

def log_recur(n: float) -> int:
    """对数阶(递归实现)"""
    if n <= 1:
        return 0
    return log_recur(n / 2) + 1

Go

/* 对数阶(递归实现)*/
func logRecur(n float64) int {
    if n <= 1 {
        return 0
    }
    return logRecur(n/2) + 1
}

JavaScript

/* 对数阶(递归实现) */
function logRecur(n) {
    if (n <= 1) return 0;
    return logRecur(n / 2) + 1;
}

TypeScript

/* 对数阶(递归实现) */
function logRecur(n: number): number {
    if (n <= 1) return 0;
    return logRecur(n / 2) + 1;
}

C

/* 对数阶(递归实现) */
int logRecur(float n) {
    if (n <= 1)
        return 0;
    return logRecur(n / 2) + 1;
}

C#

/* 对数阶(递归实现) */
int logRecur(float n) {
    if (n <= 1) return 0;
    return logRecur(n / 2) + 1;
}

Swift

/* 对数阶(递归实现) */
func logRecur(n: Double) -> Int {
    if n <= 1 {
        return 0
    }
    return logRecur(n: n / 2) + 1
}

Zig

// 对数阶(递归实现)
fn logRecur(n: f32) i32 {
    if (n <= 1) return 0;
    return logRecur(n / 2) + 1;
}

线性对数阶 $O(n \log n)$

线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。

主流排序算法的时间复杂度通常为 $O(n \log n)$ ,例如快速排序、归并排序、堆排序等。

Java

/* 线性对数阶 */
int linearLogRecur(float n) {
    if (n <= 1)
        return 1;
    int count = linearLogRecur(n / 2) +
            linearLogRecur(n / 2);
    for (int i = 0; i < n; i++) {
        count++;
    }
    return count;
}

C++

/* 线性对数阶 */
int linearLogRecur(float n) {
    if (n <= 1)
        return 1;
    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
    for (int i = 0; i < n; i++) {
        count++;
    }
    return count;
}

Python

def linear_log_recur(n: float) -> int:
    """线性对数阶"""
    if n <= 1:
        return 1
    count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)
    for _ in range(n):
        count += 1
    return count

Go

/* 线性对数阶 */
func linearLogRecur(n float64) int {
    if n <= 1 {
        return 1
    }
    count := linearLogRecur(n/2) +
        linearLogRecur(n/2)
    for i := 0.0; i < n; i++ {
        count++
    }
    return count
}

JavaScript

/* 线性对数阶 */
function linearLogRecur(n) {
    if (n <= 1) return 1;
    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
    for (let i = 0; i < n; i++) {
        count++;
    }
    return count;
}

TypeScript

/* 线性对数阶 */
function linearLogRecur(n: number): number {
    if (n <= 1) return 1;
    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
    for (let i = 0; i < n; i++) {
        count++;
    }
    return count;
}

C

/* 线性对数阶 */
int linearLogRecur(float n) {
    if (n <= 1)
        return 1;
    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
    for (int i = 0; i < n; i++) {
        count++;
    }
    return count;
}

C#

/* 线性对数阶 */
int linearLogRecur(float n) {
    if (n <= 1) return 1;
    int count = linearLogRecur(n / 2) +
                linearLogRecur(n / 2);
    for (int i = 0; i < n; i++) {
        count++;
    }
    return count;
}

Swift

/* 线性对数阶 */
func linearLogRecur(n: Double) -> Int {
    if n <= 1 {
        return 1
    }
    var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)
    for _ in stride(from: 0, to: n, by: 1) {
        count += 1
    }
    return count
}

Zig

// 线性对数阶
fn linearLogRecur(n: f32) i32 {
    if (n <= 1) return 1;
    var count: i32 = linearLogRecur(n / 2) +
                linearLogRecur(n / 2);
    var i: f32 = 0;
    while (i < n) : (i += 1) {
        count += 1;
    }
    return count;
}

线性对数阶的时间复杂度

阶乘阶 $O(n!)$

阶乘阶对应数学上的「全排列」问题。给定 $n$ 个互不重复的元素,求其所有可能的排列方案,方案数量为:

$$ n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1 $$

阶乘通常使用递归实现。例如以下代码,第一层分裂出 $n$ 个,第二层分裂出 $n - 1$ 个,以此类推,直至第 $n$ 层时终止分裂。

Java

/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
    if (n == 0)
        return 1;
    int count = 0;
    // 从 1 个分裂出 n 个
    for (int i = 0; i < n; i++) {
        count += factorialRecur(n - 1);
    }
    return count;
}

C++

/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
    if (n == 0)
        return 1;
    int count = 0;
    // 从 1 个分裂出 n 个
    for (int i = 0; i < n; i++) {
        count += factorialRecur(n - 1);
    }
    return count;
}

Python

def factorial_recur(n: int) -> int:
    """阶乘阶(递归实现)"""
    if n == 0:
        return 1
    count: int = 0
    # 从 1 个分裂出 n 个
    for _ in range(n):
        count += factorial_recur(n - 1)
    return count

Go

/* 阶乘阶(递归实现) */
func factorialRecur(n int) int {
    if n == 0 {
        return 1
    }
    count := 0
    // 从 1 个分裂出 n 个
    for i := 0; i < n; i++ {
        count += factorialRecur(n - 1)
    }
    return count
}

JavaScript

/* 阶乘阶(递归实现) */
function factorialRecur(n) {
    if (n == 0) return 1;
    let count = 0;
    // 从 1 个分裂出 n 个
    for (let i = 0; i < n; i++) {
        count += factorialRecur(n - 1);
    }
    return count;
}

TypeScript

/* 阶乘阶(递归实现) */
function factorialRecur(n: number): number {
    if (n == 0) return 1;
    let count = 0;
    // 从 1 个分裂出 n 个
    for (let i = 0; i < n; i++) {
        count += factorialRecur(n - 1);
    }
    return count;
}

C

/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
    if (n == 0)
        return 1;
    int count = 0;
    for (int i = 0; i < n; i++) {
        count += factorialRecur(n - 1);
    }
    return count;
}

C#

/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
    if (n == 0) return 1;
    int count = 0;
    // 从 1 个分裂出 n 个
    for (int i = 0; i < n; i++) {
        count += factorialRecur(n - 1);
    }
    return count;
}

Swift

/* 阶乘阶(递归实现) */
func factorialRecur(n: Int) -> Int {
    if n == 0 {
        return 1
    }
    var count = 0
    // 从 1 个分裂出 n 个
    for _ in 0 ..< n {
        count += factorialRecur(n: n - 1)
    }
    return count
}

Zig

// 阶乘阶(递归实现)
fn factorialRecur(n: i32) i32 {
    if (n == 0) return 1;
    var count: i32 = 0;
    var i: i32 = 0;
    // 从 1 个分裂出 n 个
    while (i < n) : (i += 1) {
        count += factorialRecur(n - 1);
    }
    return count;
}

阶乘阶的时间复杂度

最差、最佳、平均时间复杂度

某些算法的时间复杂度不是固定的,而是与输入数据的分布有关。例如,假设输入一个长度为 $n$ 的数组 nums ,其中 nums 由从 $1$ 至 $n$ 的数字组成,但元素顺序是随机打乱的;算法的任务是返回元素 $1$ 的索引。我们可以得出以下结论:

“函数渐近上界”使用大 $O$ 记号表示,代表「最差时间复杂度」。相应地,“函数渐近下界”用 $\Omega$ 记号来表示,代表「最佳时间复杂度」。

Java

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
int[] randomNumbers(int n) {
    Integer[] nums = new Integer[n];
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (int i = 0; i < n; i++) {
        nums[i] = i + 1;
    }
    // 随机打乱数组元素
    Collections.shuffle(Arrays.asList(nums));
    // Integer[] -> int[]
    int[] res = new int[n];
    for (int i = 0; i < n; i++) {
        res[i] = nums[i];
    }
    return res;
}

/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (nums[i] == 1)
            return i;
    }
    return -1;
}

C++

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
vector<int> randomNumbers(int n) {
    vector<int> nums(n);
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (int i = 0; i < n; i++) {
        nums[i] = i + 1;
    }
    // 使用系统时间生成随机种子
    unsigned seed = chrono::system_clock::now().time_since_epoch().count();
    // 随机打乱数组元素
    shuffle(nums.begin(), nums.end(), default_random_engine(seed));
    return nums;
}

/* 查找数组 nums 中数字 1 所在索引 */
int findOne(vector<int> &nums) {
    for (int i = 0; i < nums.size(); i++) {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (nums[i] == 1)
            return i;
    }
    return -1;
}

Python

def random_numbers(n: int) -> list[int]:
    """生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱"""
    # 生成数组 nums =: 1, 2, 3, ..., n
    nums: list[int] = [i for i in range(1, n + 1)]
    # 随机打乱数组元素
    random.shuffle(nums)
    return nums

def find_one(nums: list[int]) -> int:
    """查找数组 nums 中数字 1 所在索引"""
    for i in range(len(nums)):
        # 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        # 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if nums[i] == 1:
            return i
    return -1

Go

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
func randomNumbers(n int) []int {
    nums := make([]int, n)
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for i := 0; i < n; i++ {
        nums[i] = i + 1
    }
    // 随机打乱数组元素
    rand.Shuffle(len(nums), func(i, j int) {
        nums[i], nums[j] = nums[j], nums[i]
    })
    return nums
}

/* 查找数组 nums 中数字 1 所在索引 */
func findOne(nums []int) int {
    for i := 0; i < len(nums); i++ {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if nums[i] == 1 {
            return i
        }
    }
    return -1
}

JavaScript

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
function randomNumbers(n) {
    const nums = Array(n);
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (let i = 0; i < n; i++) {
        nums[i] = i + 1;
    }
    // 随机打乱数组元素
    for (let i = 0; i < n; i++) {
        const r = Math.floor(Math.random() * (i + 1));
        const temp = nums[i];
        nums[i] = nums[r];
        nums[r] = temp;
    }
    return nums;
}

/* 查找数组 nums 中数字 1 所在索引 */
function findOne(nums) {
    for (let i = 0; i < nums.length; i++) {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (nums[i] === 1) {
            return i;
        }
    }
    return -1;
}

TypeScript

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
function randomNumbers(n: number): number[] {
    const nums = Array(n);
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (let i = 0; i < n; i++) {
        nums[i] = i + 1;
    }
    // 随机打乱数组元素
    for (let i = 0; i < n; i++) {
        const r = Math.floor(Math.random() * (i + 1));
        const temp = nums[i];
        nums[i] = nums[r];
        nums[r] = temp;
    }
    return nums;
}

/* 查找数组 nums 中数字 1 所在索引 */
function findOne(nums: number[]): number {
    for (let i = 0; i < nums.length; i++) {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (nums[i] === 1) {
            return i;
        }
    }
    return -1;
}

C

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
int *randomNumbers(int n) {
    // 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int)
    int *nums = (int *)malloc(n * sizeof(int));
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (int i = 0; i < n; i++) {
        nums[i] = i + 1;
    }
    // 随机打乱数组元素
    for (int i = n - 1; i > 0; i--) {
        int j = rand() % (i + 1);
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    return nums;
}

/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int *nums, int n) {
    for (int i = 0; i < n; i++) {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (nums[i] == 1)
            return i;
    }
    return -1;
}

C#

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
int[] randomNumbers(int n) {
    int[] nums = new int[n];
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (int i = 0; i < n; i++) {
        nums[i] = i + 1;
    }

    // 随机打乱数组元素
    for (int i = 0; i < nums.Length; i++) {
        var index = new Random().Next(i, nums.Length);
        var tmp = nums[i];
        var ran = nums[index];
        nums[i] = ran;
        nums[index] = tmp;
    }
    return nums;
}

/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int[] nums) {
    for (int i = 0; i < nums.Length; i++) {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (nums[i] == 1)
            return i;
    }
    return -1;
}

Swift

/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
func randomNumbers(n: Int) -> [Int] {
    // 生成数组 nums = { 1, 2, 3, ..., n }
    var nums = Array(1 ... n)
    // 随机打乱数组元素
    nums.shuffle()
    return nums
}

/* 查找数组 nums 中数字 1 所在索引 */
func findOne(nums: [Int]) -> Int {
    for i in nums.indices {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if nums[i] == 1 {
            return i
        }
    }
    return -1
}

Zig

// 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱
pub fn randomNumbers(comptime n: usize) [n]i32 {
    var nums: [n]i32 = undefined;
    // 生成数组 nums = { 1, 2, 3, ..., n }
    for (nums) |*num, i| {
        num.* = @intCast(i32, i) + 1;
    }
    // 随机打乱数组元素
    const rand = std.crypto.random;
    rand.shuffle(i32, &nums);
    return nums;
}

// 查找数组 nums 中数字 1 所在索引
pub fn findOne(nums: []i32) i32 {
    for (nums) |num, i| {
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
        if (num == 1) return @intCast(i32, i);
    }
    return -1;
}

提示

实际应用中我们很少使用「最佳时间复杂度」,因为通常只有在很小概率下才能达到,可能会带来一定的误导性。相反,「最差时间复杂度」更为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。

从上述示例可以看出,最差或最佳时间复杂度只出现在“特殊分布的数据”中,这些情况的出现概率可能很小,因此并不能最真实地反映算法运行效率。相较之下,「平均时间复杂度」可以体现算法在随机输入数据下的运行效率,用 $\Theta$ 记号来表示。

对于部分算法,我们可以简单地推算出随机数据分布下的平均情况。比如上述示例,由于输入数组是被打乱的,因此元素 $1$ 出现在任意索引的概率都是相等的,那么算法的平均循环次数则是数组长度的一半 $\frac{n}{2}$ ,平均时间复杂度为 $\Theta(\frac{n}{2}) = \Theta(n)$ 。

但在实际应用中,尤其是较为复杂的算法,计算平均时间复杂度比较困难,因为很难简便地分析出在数据分布下的整体数学期望。在这种情况下,我们通常使用最差时间复杂度作为算法效率的评判标准。

问题:为什么很少看到 $\Theta$ 符号?

可能由于 $O$ 符号过于朗朗上口,我们常常使用它来表示「平均复杂度」,但从严格意义上看,这种做法并不规范。在本书和其他资料中,若遇到类似“平均时间复杂度 $O(n)$”的表述,请将其直接理解为 $\Theta(n)$ 。