亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++ 實(shí)現(xiàn)桶排序的示例代碼

 更新時(shí)間:2021年07月12日 11:41:57   作者:新世紀(jì)debug戰(zhàn)士  
桶排序或所謂的箱排序,是一個(gè)排序算法,工作的原理是將數(shù)組分到有限數(shù)量的桶子,本文詳細(xì)的介紹了如何實(shí)現(xiàn),感興趣的可以了解一下

桶排序:整數(shù) 

原理

原理簡(jiǎn)述:按照需要排序數(shù)組的實(shí)際情況,生成一個(gè)一定長(zhǎng)度的一維數(shù)組,用于統(tǒng)計(jì)需要排序數(shù)組的不同數(shù)值的重復(fù)次數(shù),完成統(tǒng)計(jì)后,再按順序重復(fù)輸出該數(shù)值

實(shí)現(xiàn)步驟:

  • 確定需要排序數(shù)組的最大值和最小值
  • 生成桶數(shù)組,并初始化
  • 對(duì)需要排序數(shù)組進(jìn)行統(tǒng)計(jì),統(tǒng)計(jì)結(jié)果放入相應(yīng)的桶中
  • 循環(huán)輸出桶,并替換原序列

模擬生成整數(shù)隨機(jī)數(shù)

#include <random>
#include <ctime>
 
// 傳入空數(shù)組arr[]以及它的長(zhǎng)度len,填入[min, max]區(qū)間內(nèi)的隨機(jī)整數(shù)
void getRand(int arr[], int len, int min, int max) {
    std::default_random_engine e;
    e.seed(time(0));
    std::uniform_int_distribution<int> u(min,max);
    for (int i = 0; i < len; i++) arr[i] = u(e);
}

桶排序?qū)崿F(xiàn)

#include <climits>
 
void bucketSort(int arr[], int len) {
    // 確定最大值和最小值
    int max = INT_MIN; int min = INT_MAX;
    for (int i = 0; i < len; i++) {
        if (arr[i] > max) max = arr[i];
        if (arr[i] < min) min = arr[i];
    }
 
    // 生成桶數(shù)組
    // 設(shè)置最小的值為索引0,每個(gè)桶間隔為1
    int bucketLen = max - min + 1;
    // 初始化桶
    int bucket[bucketLen];
    for (int i = 0; i < bucketLen; i++) bucket[i] = 0;
 
    // 放入桶中
    int index = 0;
    for (int i = 0; i < len; i++) {
        index = arr[i] - min;
        bucket[index] += 1;
    }
 
    // 替換原序列
    int start = 0;
    for (int i = 0; i < bucketLen; i++) {
        for (int j = start; j < start + bucket[i]; j++) {
            arr[j] = min + i;
        }
        start += bucket[i];
    }
}

完整版可運(yùn)行程序

#include <iostream>
#include <random>
#include <ctime>
#include <climits>
 
// 一些參數(shù)
const int MAX = 30;
const int LEN = 64;
 
void bucketSort(int arr[], int len);
void getRand(int arr[], int len, int min, int max);
 
int main() {
    int arr[LEN] = {0};
 
    // 產(chǎn)生隨機(jī)值
    getRand(arr,LEN,0,MAX);
 
    // 打印隨機(jī)值
    std::cout << "Before sorted:" << std::endl;
    for (int i : arr) {
        std::cout << i << " ";
    }
    std::cout << "" << std::endl;
 
    // 排序
    bucketSort(arr,LEN);
 
    // 打印輸出值
    std::cout << "After sorted:" << std::endl;
    for (int i : arr) {
        std::cout << i << " ";
    }
}
 
void getRand(int arr[], int len, int min, int max) {
    std::default_random_engine e;
    e.seed(time(0));
    std::uniform_int_distribution<int> u(min,max);
    for (int i = 0; i < len; i++) arr[i] = u(e);
}
 
void bucketSort(int arr[], int len) {
    // 確定最大值和最小值
    int max = INT_MIN; int min = INT_MAX;
    for (int i = 0; i < len; i++) {
        if (arr[i] > max) max = arr[i];
        if (arr[i] < min) min = arr[i];
    }
 
    // 生成桶數(shù)組
    // 設(shè)置最小的值為索引0,每個(gè)桶間隔為1
    int bucketLen = max - min + 1;
    // 初始化桶
    int bucket[bucketLen];
    for (int i = 0; i < bucketLen; i++) bucket[i] = 0;
 
    // 放入桶中
    int index = 0;
    for (int i = 0; i < len; i++) {
        index = arr[i] - min;
        bucket[index] += 1;
    }
 
    // 替換原序列
    int start = 0;
    for (int i = 0; i < bucketLen; i++) {
        for (int j = start; j < start + bucket[i]; j++) {
            arr[j] = min + i;
        }
        start += bucket[i];
    }
}

結(jié)果 

時(shí)間復(fù)雜度計(jì)算

分析算法步驟:

  • 確定需要排序數(shù)組的最大值和最小值 - 循環(huán)len次
  • 生成桶數(shù)組,并初始化 - 循環(huán)bucketLen次
  • 對(duì)需要排序數(shù)組進(jìn)行統(tǒng)計(jì),統(tǒng)計(jì)結(jié)果放入相應(yīng)的桶中 - 循環(huán)len次
  • 循環(huán)輸出桶,并替換原序列 - 循環(huán)bucketLen+len次

總時(shí)間復(fù)雜度為 O(3*len + 2*bucketLen) .

P.S. 浮點(diǎn)型的桶排序,由于考慮到每個(gè)桶之間區(qū)間間隔難以確定、每個(gè)桶內(nèi)儲(chǔ)存的值數(shù)量不定等情況,筆者目前尚無(wú)法通過基礎(chǔ)的C++運(yùn)算來實(shí)現(xiàn),使用一些高級(jí)功能又怕時(shí)間復(fù)雜度爆炸,最終得不償失,不如轉(zhuǎn)而研究其他類型的排序方法更值得。如果有大佬寫出來浮點(diǎn)型桶排序,可以評(píng)論或者私信我,感謝!

** 參考書籍:啊哈!算法

到此這篇關(guān)于C++ 實(shí)現(xiàn)桶排序的示例代碼的文章就介紹到這了,更多相關(guān)C++ 桶排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論