
- 資料結構與演算法
- DSA - 首頁
- DSA - 概述
- DSA - 環境搭建
- DSA - 演算法基礎
- DSA - 漸進分析
- 資料結構
- DSA - 資料結構基礎
- DSA - 資料結構和型別
- DSA - 陣列資料結構
- 連結串列
- DSA - 連結串列資料結構
- DSA - 雙向連結串列資料結構
- DSA - 迴圈連結串列資料結構
- 棧與佇列
- DSA - 棧資料結構
- DSA - 表示式解析
- DSA - 佇列資料結構
- 搜尋演算法
- DSA - 搜尋演算法
- DSA - 線性查詢演算法
- DSA - 二分查詢演算法
- DSA - 插值查詢
- DSA - 跳躍查詢演算法
- DSA - 指數查詢
- DSA - 斐波那契查詢
- DSA - 子列表搜尋
- DSA - 雜湊表
- 排序演算法
- DSA - 排序演算法
- DSA - 氣泡排序演算法
- DSA - 插入排序演算法
- DSA - 選擇排序演算法
- DSA - 歸併排序演算法
- DSA - 希爾排序演算法
- DSA - 堆排序
- DSA - 桶排序演算法
- DSA - 計數排序演算法
- DSA - 基數排序演算法
- DSA - 快速排序演算法
- 圖資料結構
- DSA - 圖資料結構
- DSA - 深度優先遍歷
- DSA - 廣度優先遍歷
- DSA - 生成樹
- 樹資料結構
- DSA - 樹資料結構
- DSA - 樹遍歷
- DSA - 二叉搜尋樹
- DSA - AVL樹
- DSA - 紅黑樹
- DSA - B樹
- DSA - B+樹
- DSA - 伸展樹
- DSA - 字典樹
- DSA - 堆資料結構
- 遞迴
- DSA - 遞迴演算法
- DSA - 使用遞迴的漢諾塔
- DSA - 使用遞迴的斐波那契數列
- 分治法
- DSA - 分治法
- DSA - 最大最小問題
- DSA - Strassen矩陣乘法
- DSA - Karatsuba演算法
- 貪心演算法
- DSA - 貪心演算法
- DSA - 旅行商問題(貪心法)
- DSA - Prim最小生成樹
- DSA - Kruskal最小生成樹
- DSA - Dijkstra最短路徑演算法
- DSA - 地圖著色演算法
- DSA - 分數揹包問題
- DSA - 帶截止日期的作業排序
- DSA - 最優合併模式演算法
- 動態規劃
- DSA - 動態規劃
- DSA - 矩陣鏈乘法
- DSA - Floyd Warshall演算法
- DSA - 0-1揹包問題
- DSA - 最長公共子序列演算法
- DSA - 旅行商問題(動態規劃法)
- 近似演算法
- DSA - 近似演算法
- DSA - 頂點覆蓋演算法
- DSA - 集合覆蓋問題
- DSA - 旅行商問題(近似演算法)
- 隨機化演算法
- DSA - 隨機化演算法
- DSA - 隨機化快速排序演算法
- DSA - Karger最小割演算法
- DSA - Fisher-Yates洗牌演算法
- DSA有用資源
- DSA - 問答
- DSA - 快速指南
- DSA - 有用資源
- DSA - 討論
桶排序演算法
桶排序演算法類似於計數排序演算法,因為它只是計數排序的廣義形式。桶排序假設輸入元素是從區間[0, 1)上的均勻分佈中抽取的。
因此,桶排序演算法將區間[0, 1)分成'n'個相等的部分,輸入元素被新增到索引為桶的索引中,其中索引基於(n × 元素)值的較低邊界。由於該演算法假設值為在較小範圍內均勻分佈的獨立數字,因此不會有很多元素只落入一個桶中。
例如,讓我們來看一個輸入元素列表:0.08, 0.01, 0.19, 0.89, 0.34, 0.07, 0.30, 0.82, 0.39, 0.45, 0.36。桶排序將如下所示:

桶排序演算法
讓我們看看下面這個演算法將如何繼續進行:
步驟1 - 將區間分成'n'個相等的部分,每個部分稱為一個桶。如果n為10,則有10個桶;否則更多。
步驟2 - 從輸入陣列A中獲取輸入元素,並根據計算公式B[i]= $\lfloor$n.A[i]$\rfloor$將它們新增到這些輸出桶B中。
步驟3 - 如果有任何元素被新增到已經佔用的桶中,則透過相應的桶建立一個連結串列。
步驟4 - 然後我們應用插入排序對每個桶中的所有元素進行排序。
步驟5 - 這些桶被連線在一起,最終得到輸出。
虛擬碼
BUCKET-SORT(A) let B[0 … n – 1] be a new array n = A.length for i = 0 to n – 1 make B[i] an empty list for i = 1 to n insert A[i] into list B[$\lfloor$𝑛.𝐴[𝑖]$\rfloor$] for i = 0 to n – 1 sort list B[i] with insertion sort concatenate the lists B[0], B[1]; ………… ; B[n – 1] together in order
分析
桶排序演算法假設輸入的獨立性,因此該演算法的平均時間複雜度為Θ(n)
示例
考慮一個輸入元素列表:0.78, 0.17, 0.93, 0.39, 0.26, 0.72, 0.21, 0.12, 0.33, 0.28,使用桶排序對這些元素進行排序:
解答
步驟1
線性地從輸入陣列的索引'0'插入所有元素。也就是說,我們先插入0.78,然後依次插入其他元素。插入元素的位置使用公式計算:B[i]= $\lfloor$n.A[i]$\rfloor$,即$\lfloor$10 ×0.78$\rfloor$=7

現在,我們將0.17插入索引$\lfloor$10 ×0.17$\rfloor$=1

步驟3
將下一個元素0.93插入到$\lfloor$10 ×0.93$\rfloor$=9索引處的輸出桶中

步驟4
使用公式$\lfloor$10 ×0.39$\rfloor$=3將0.39插入索引3

步驟5
將輸入陣列中的下一個元素0.26插入到$\lfloor$10 ×0.26$\rfloor$=2位置

步驟6
這裡比較棘手。現在,輸入列表中的下一個元素是0.72,需要使用公式$\lfloor$10 ×0.72$\rfloor$=7將其插入索引'7'。但是索引'7'的桶中已經存在一個數字。因此,從索引'7'建立一個連結來儲存新的數字,就像一個連結串列一樣,如下所示:

步驟7
以類似的方式將剩餘的數字新增到桶中,透過所需桶建立連結串列。但是,在將這些元素作為列表插入時,我們應用插入排序,即比較兩個元素並將最小值新增到前面,如下所示:

步驟8
現在,要獲得輸出,將所有桶連線在一起。
0.12, 0.17, 0.21, 0.26, 0.28, 0.33, 0.39, 0.72, 0.78, 0.93
實現
桶排序演算法的實現首先獲取陣列的最大元素並確定輸出的桶大小。元素根據一些計算插入到這些桶中。
在本教程中,我們使用四種程式語言執行桶排序。
#include <stdio.h> void bucketsort(int a[], int n){ // function to implement bucket sort int max = a[0]; // get the maximum element in the array for (int i = 1; i < n; i++) if (a[i] > max) max = a[i]; int b[max], i; for (int i = 0; i <= max; i++) { b[i] = 0; } for (int i = 0; i < n; i++) { b[a[i]]++; } for (int i = 0, j = 0; i <= max; i++) { while (b[i] > 0) { a[j++] = i; b[i]--; } } } int main(){ int a[] = {12, 45, 33, 87, 56, 9, 11, 7, 67}; int n = sizeof(a) / sizeof(a[0]); // n is the size of array printf("Before sorting array elements are: \n"); for (int i = 0; i < n; ++i) printf("%d ", a[i]); bucketsort(a, n); printf("\nAfter sorting array elements are: \n"); for (int i = 0; i < n; ++i) printf("%d ", a[i]); }
輸出
Before sorting array elements are: 12 45 33 87 56 9 11 7 67 After sorting array elements are: 7 9 11 12 33 45 56 67 87
#include <iostream> using namespace std; void bucketsort(int a[], int n){ // function to implement bucket sort int max = a[0]; // get the maximum element in the array for (int i = 1; i < n; i++) if (a[i] > max) max = a[i]; int b[max], i; for (int i = 0; i <= max; i++) { b[i] = 0; } for (int i = 0; i < n; i++) { b[a[i]]++; } for (int i = 0, j = 0; i <= max; i++) { while (b[i] > 0) { a[j++] = i; b[i]--; } } } int main(){ int a[] = {12, 45, 33, 87, 56, 9, 11, 7, 67}; int n = sizeof(a) / sizeof(a[0]); // n is the size of array cout << "Before sorting array elements are: \n"; for (int i = 0; i < n; ++i) cout << a[i] << " "; bucketsort(a, n); cout << "\nAfter sorting array elements are: \n"; for (int i = 0; i < n; ++i) cout << a[i] << " "; }
輸出
Before sorting array elements are: 12 45 33 87 56 9 11 7 67 After sorting array elements are: 7 9 11 12 33 45 56 67 87
import java.io.*; import java.util.*; public class BucketSort { static void bucketsort(int a[], int n) { // function to implement bucket sort int max = a[0]; // get the maximum element in the array for (int i = 1; i < n; i++) if (a[i] > max) max = a[i]; int b[] = new int[max+1]; for (int i = 0; i <= max; i++) { b[i] = 0; } for (int i = 0; i < n; i++) { b[a[i]]++; } for (int i = 0, j = 0; i <= max; i++) { while (b[i] > 0) { a[j++] = i; b[i]--; } } } public static void main(String args[]) { int n = 9; int a[] = {12, 45, 33, 87, 56, 9, 11, 7, 67}; System.out.println("Before sorting array elements are: "); for (int i = 0; i < n; ++i) System.out.print(a[i] + " "); bucketsort(a, n); System.out.println("\nAfter sorting array elements are: "); for (int i = 0; i < n; ++i) System.out.print(a[i] + " "); } }
輸出
Before sorting array elements are: 12 45 33 87 56 9 11 7 67 After sorting array elements are: 7 9 11 12 33 45 56 67 87
def bucketsort(a, n): max_val = max(a) b = [0] * (max_val + 1) for i in range(n): b[a[i]] += 1 j = 0 for i in range(max_val + 1): while b[i] > 0: a[j] = i j += 1 b[i] -= 1 a = [12, 45, 33, 87, 56, 9, 11, 7, 67] n = len(a) print("Before sorting array elements are: ") print(a) bucketsort(a, n) print("\nAfter sorting array elements are: ") print(a)
輸出
Before sorting array elements are: [12, 45, 33, 87, 56, 9, 11, 7, 67] After sorting array elements are: [7, 9, 11, 12, 33, 45, 56, 67, 87]