0% found this document useful (0 votes)
34 views7 pages

Bucket Sort

Data structures

Uploaded by

davintivah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Bucket Sort

Data structures

Uploaded by

davintivah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Group 14 Members: Daniella Wangari DCSC01/3896/2022, Thiong`o Anjeline

DCSC01/6657/2020, Thiong`o Davin T006 / 301731/ 2023

A) Mechanism/Concepts of Bucket Sort

Bucket Sort is a distribution sorting algorithm that works by dividing an array into a number of

buckets. Each bucket is then sorted individually, either using another sorting algorithm or by

recursively applying the bucket sort. The sorted buckets are then concatenated to form the final

sorted array. This method is particularly effective for uniformly distributed data and when the

data is evenly spread across a bucket. The average time complexity of Bucket Sort is O(n+k),

where n is the number of elements and k is the number of buckets.

B) Example of the Mechanism

Consider an array of n elements: [0.42, 0.32, 0.53, 0.21, 0.25, 0.47, 0.34]

1. Step 1: Create Buckets Create an empty array of buckets.

2. Step 2: Distribute Elements Distribute the elements of the array into the respective

buckets. For example:

o Bucket 1: [0.21, 0.25]

o Bucket 2: [0.32, 0.34]

o Bucket 3: [0.42, 0.47]

o Bucket 4: [0.53]

 Step 3: Sort Individual Buckets Sort each bucket using another sorting algorithm such as
Insertion Sort.
 Step 4: Concatenate Buckets Concatenate the sorted buckets to get the final sorted array.

Illustration

Original Array: [0.42, 0.32, 0.53, 0.21, 0.25, 0.47, 0.34]

Step 1: Create Buckets

Buckets: [[], [], [], []]

Step 2: Distribute Elements

Buckets: [[0.21, 0.25], [0.32, 0.34], [0.42, 0.47], [0.53]]

Step 3: Sort Individual Buckets

Buckets: [[0.21, 0.25], [0.32, 0.34], [0.42, 0.47], [0.53]]

Step 4: Concatenate Buckets

Sorted Array: [0.21, 0.25, 0.32, 0.34, 0.42, 0.47, 0.53]

C) Algorithm/Pseudocode

BucketSort(array, n)

create n empty buckets

for all elements in array

insert array[i] into bucket based on element value

for all buckets

sort individual buckets

concatenate all buckets into array

D) Implementation Using C++ CPP

#include <iostream>

#include <vector>
#include <algorithm>

using namespace std;

void bucketSort(vector<float>& arr) {

int n = arr.size();

vector<vector<float>> buckets(n); // Corrected to use vector<vector<float>>

for (int i = 0; i < n; i++) {

int idx = n * arr[i];

buckets[idx].push_back(arr[i]); }

for (int i = 0; i < n; i++) {

sort(buckets[i].begin(), buckets[i].end()); }

int index = 0;

for (int i = 0; i < n; i++) {

for (size_t j = 0; j < buckets[i].size(); j++) { // Changed int to size_t

arr[index++] = buckets[i][j]; }

int main() {

vector<float> arr = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};


bucketSort(arr);

cout << "Sorted array is: ";

for (size_t i = 0; i < arr.size(); i++) { // Changed int to size_t

cout << arr[i] << " "; }

cout << endl;

return 0; }

E) Implementation Using Java

import java.util.*;

public class BucketSort {

public static void bucketSort(float[] arr) {

int n = arr.length;

List<Float>[] buckets = new List[n];

for (int i = 0; i < n; i++) {

buckets[i] = new ArrayList<Float>(); }

for (int i = 0; i < n; i++) {

int idx = (int) (arr[i] * n);

buckets[idx].add(arr[i]); }

for (int i = 0; i < n; i++) {


Collections.sort(buckets[i]); }

int index = 0;

for (int i = 0; i < n; i++) {

for (Float num : buckets[i]) {

arr[index++] = num; }

public static void main(String[] args) {

float[] arr = {0.42f, 0.32f, 0.53f, 0.21f, 0.25f, 0.47f, 0.34f};

bucketSort(arr);

System.out.println("Sorted array: " + Arrays.toString(arr)); }

F) Applications, Advantages, and Disadvantages of Bucket Sort

Applications:

 Sorting Uniformly Distributed Data:

 Bucket Sort excels when dealing with data that is uniformly distributed over a range. This

makes it ideal for sorting floating-point numbers in a specific interval, such as grades,

percentages, or any other normalized data sets.

 Digital Image Processing:


 In image processing, Bucket Sort can be used for tasks like histogram-based operations

where pixel values need to be sorted and grouped. It's especially useful in algorithms that

require sorting pixel intensity values efficiently.

 Parallel Processing:

 Bucket Sort can be easily parallelized, making it suitable for use in distributed computing

environments. Each bucket can be processed independently, allowing for efficient use of

multiple processors or machines.

 Computer Graphics:

 In computer graphics, Bucket Sort can help in tasks such as rendering or sorting objects

based on depth (z-buffer sorting). It ensures that objects are rendered in the correct order

for accurate visual representation.

 Financial Analysis:

 For sorting financial data like stock prices, transaction amounts, or other numerical data,

Bucket Sort can be used to achieve fast and efficient results, particularly when the data

falls within a known range.

 Big Data and Machine Learning:

 In scenarios involving large datasets, such as big data applications or preprocessing steps

in machine learning pipelines, Bucket Sort can be employed to quickly sort and organize

data before further analysis.

Advantages and Disadvantages of Bucket Sort

Advantages:
1. Efficiency: When the input is uniformly distributed, Bucket Sort can achieve linear time

complexity, making it very efficient compared to other sorting algorithms.

2. Simplicity: The algorithm is simple and easy to understand. It’s straightforward to implement,

especially for basic cases.

3. Performance on Large Datasets: For datasets that are well-suited to the algorithm, Bucket Sort

can be incredibly fast, often outperforming traditional comparison-based sorting algorithms like

Quick Sort or Merge Sort.

4. Parallelism: Bucket Sort can be easily parallelized since each bucket can be sorted

independently. This makes it suitable for multi-threaded or distributed computing environments.

Disadvantages:

1. Non-Uniform Distribution: The performance can degrade if the input is not uniformly

distributed, leading to buckets with very uneven sizes. This can make the algorithm less efficient

than expected.

2. Overhead: There is additional overhead in dividing the data into buckets and managing those

buckets, which can be inefficient for small datasets.

3. Space Complexity: Bucket Sort requires additional space to store the buckets, which means it

can use a significant amount of memory, especially when dealing with large datasets.

4. Limited Application: It is most effective for data that falls within a known, finite range. It may

not perform as well with arbitrary or unknown ranges of data.

You might also like