C++ 實現(xiàn)桶排序的示例代碼
桶排序:整數(shù)
原理
原理簡述:按照需要排序數(shù)組的實際情況,生成一個一定長度的一維數(shù)組,用于統(tǒng)計需要排序數(shù)組的不同數(shù)值的重復(fù)次數(shù),完成統(tǒng)計后,再按順序重復(fù)輸出該數(shù)值
實現(xiàn)步驟:
- 確定需要排序數(shù)組的最大值和最小值
- 生成桶數(shù)組,并初始化
- 對需要排序數(shù)組進行統(tǒng)計,統(tǒng)計結(jié)果放入相應(yīng)的桶中
- 循環(huán)輸出桶,并替換原序列
模擬生成整數(shù)隨機數(shù)
#include <random> #include <ctime> // 傳入空數(shù)組arr[]以及它的長度len,填入[min, max]區(qū)間內(nèi)的隨機整數(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,每個桶間隔為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]; } }
完整版可運行程序
#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)生隨機值 getRand(arr,LEN,0,MAX); // 打印隨機值 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,每個桶間隔為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é)果
時間復(fù)雜度計算
分析算法步驟:
- 確定需要排序數(shù)組的最大值和最小值 - 循環(huán)len次
- 生成桶數(shù)組,并初始化 - 循環(huán)bucketLen次
- 對需要排序數(shù)組進行統(tǒng)計,統(tǒng)計結(jié)果放入相應(yīng)的桶中 - 循環(huán)len次
- 循環(huán)輸出桶,并替換原序列 - 循環(huán)bucketLen+len次
總時間復(fù)雜度為 O(3*len + 2*bucketLen) .
P.S. 浮點型的桶排序,由于考慮到每個桶之間區(qū)間間隔難以確定、每個桶內(nèi)儲存的值數(shù)量不定等情況,筆者目前尚無法通過基礎(chǔ)的C++運算來實現(xiàn),使用一些高級功能又怕時間復(fù)雜度爆炸,最終得不償失,不如轉(zhuǎn)而研究其他類型的排序方法更值得。如果有大佬寫出來浮點型桶排序,可以評論或者私信我,感謝!
** 參考書籍:啊哈!算法
到此這篇關(guān)于C++ 實現(xiàn)桶排序的示例代碼的文章就介紹到這了,更多相關(guān)C++ 桶排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式
這篇文章主要介紹了C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實例詳解
這篇文章主要介紹了C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01深入講解C++數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)函數(shù)的知識
這篇文章主要介紹了深入講解C++數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)函數(shù)的知識,包括類型轉(zhuǎn)換運算符函數(shù)等內(nèi)容,需要的朋友可以參考下2015-09-09