Data structure and algorithm interview: The time complexity of the comparison-based sorting algorithm is O(nlogn) in the worst case. Is there a faster algorithm? (hint: counting sort, radix sort)

Data structure and algorithm interview: The time complexity of the comparison-based sorting algorithm is O(nlogn) in the worst case. Is there a faster algorithm? (hint: counting sort, radix sort)

Introduction: The time complexity of the comparison-based sorting algorithm is O(nlogn) in the worst case. Is there a faster algorithm? (hint: counting sort, radix sort)

Radix sort is a sorting algorithm with time complexity O(nlogn), where d is the number of digits of the largest number in array a. Radix sorting is faster than comparison sorting if the number length d is small.

The implementation idea of ​​radix sorting is as follows:

  1. Use a bucket array to record the number of occurrences of each possible number (here assume the value range is between 0 and 9).
  2. Sort the original array a according to ones, tens, hundreds, thousands... For a certain "current number of digits", counting sorting or bucket sorting can be used. After this round of sorting, the original array a has been sorted.

The following is the code to implement radix sorting in C++, with detailed comments:

#include <iostream>
#include <vector>

using namespace std;

void radix_sort(vector<int>& a) {
    int n = a.size();
    if (n <= 1) return;

    // 获取数组中的最大值
    int max_val = a[0];
    for (int i = 1; i < n; ++i) {
        max_val = max(max_val, a[i]);
    }

    // 计算出最大值的长度
    int k = 0;
    while (max_val > 0) {
        max_val /= 10;
        ++k;
    }

    // 建立桶数组并进行基数排序
    vector<int> bucket(a.size());
    vector<int> count(10);

    // 每一轮循环按照不同的位数进行排序
    for (int i = 0, r = 1; i < k; ++i, r *= 10) {
        // 将桶清零
        fill(count.begin(), count.end(), 0);

        // 统计出现次数
        for (int j = 0; j < n; ++j) {
            int c = (a[j] / r) % 10;
            ++count[c];
        }

        // 计算前缀和
        for (int j = 1; j <= 9; ++j) {
            count[j] += count[j - 1];
        }

        // 按顺序将数放到桶中
        for (int j = n - 1; j >= 0; --j) {
            int c = (a[j] / r) % 10;
            bucket[--count[c]] = a[j];
        }

        // 从桶中取回数据
        for (int j = 0; j < n; ++j) {
            a[j] = bucket[j];
        }
    }
}

int main() {
    vector<int> a = {7, 6, 5, 4, 3, 2, 1, 0};
    radix_sort(a);
    for (int i = 0; i < a.size(); ++i) {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}

With the help of two data structures "bucket" and "count", the algorithm realizes the radix sorting algorithm with time complexity O(dn). In order to conveniently handle the numbers in the array, we can convert them to strings and then operate on them.

  • java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main {
    
    
    public static void radixSort(int[] a) {
    
    
        int n = a.length;
        if (n <= 1) return;

        // 获取数组中的最大值
        int max_val = a[0];
        for (int i = 1; i < n; ++i) {
    
    
            max_val = Math.max(max_val, a[i]);
        }

        // 计算出最大值的位数
        int k = 0;
        while (max_val > 0) {
    
    
            max_val /= 10;
            ++k;
        }

        // 建立桶数组并进行基数排序
        int[] bucket = new int[a.length];
        int[] count = new int[10];

        // 每一轮循环按照不同的位数进行排序
        for (int i = 0, r = 1; i < k; ++i, r *= 10) {
    
    
            // 将桶清零
            Arrays.fill(count, 0);

            // 统计出现次数
            for (int j = 0; j < n; ++j) {
    
    
                int c = (a[j] / r) % 10;
                ++count[c];
            }

            // 计算前缀和
            for (int j = 1; j < 10; ++j) {
    
    
                count[j] += count[j - 1];
            }

            // 按顺序将数放到桶中
            for (int j = n - 1; j >= 0; --j) {
    
    
                int c = (a[j] / r) % 10;
                bucket[--count[c]] = a[j];
            }

            // 从桶中取回数据
            for (int j = 0; j < n; ++j) {
    
    
                a[j] = bucket[j];
            }
        }
    }

    public static void main(String[] args) {
    
    
        int[] a = {
    
    7, 6, 5, 4, 3, 2, 1, 0};
        radixSort(a);
        for (int i = 0; i < a.length; ++i) {
    
    
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/131170523