×

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally, we take the elements out and join them to get the sorted result.

The bucket sort assumes that the input is generated by a random process that distributes the elements uniformly over the interval (0,1).

The idea of ??bucket sort is to divide the interval 0 to 1 into “n” equal-sized sub-intervals or buckets, and then distribute the n input number to the bucket.

The bucket sort program requires an auxiliary array X [0, 1, 2, ...., n-1] of the linked list.

Complexity table of bucket sort

ComplexityBest caseAverage caseWorst case
TimeO(n + k)O(n + k)O(n2)
Space  O(n)

Algorithm of bucket sort

Bucket Sort() 
Step 1: Create N buckets and each buckets is hold a range of values. 
Step 2: for i ? 1 to n     // initialize each bucket with 0 values. 
Step 3: for i ? 1 to n     put elements into buckets matching the range. 
Step 4: for i ? 1 to n     sort elements in each bucket.     gather elements from each bucket 
Step 5: exit

For example: Suppose we have the following array, which we have to sort.

11861252

Step 1: Create a new array, and this array size is 10. Every field of this array is used as a bucket.  

Step 2: Insert the array element into the new array bucket, and these elements will be added according to the range of the bucket.    

Step 3: Gather the element form each bucket.  

Bucket sort program in C language:

#include <stdio.h> 
 void Bucket_Sort(int array[], int n) 
 {       
 int i, j;    
    int count[n];   
    for (i = 0; i < n; i++)     
      count[i] = 0;       
 for (i = 0; i < n; i++)   
       (count[array[i]])++;   
      for (i = 0, j = 0; i < n; i++)   
         for(; count[i] > 0; (count[i])--)  
            array[j++] = i;   
} 
      int main() 
 {    
  int array[100], i, num;  
        printf("Enter the size of array : ");   
      scanf("%d", &num);  
       printf("Enter the %d elements to be sorted:\n",num); 
      for (i = 0; i < num; i++)  
        scanf("%d", &array[i]);  
     printf("\nThe array of elements before sorting : \n");  
    for (i = 0; i < num; i++)  
        printf("%d ", array[i]);  
      printf("\nThe array of elements after sorting : \n");  
     Bucket_Sort(array, num);   
    for (i = 0; i < num; i++)  
        printf("%d ", array[i]);   
      printf("\n");        
   return 0; 
 } 

Bucket sort program in java language:

import java.util.ArrayList; 
import java.util.Collections;  
 public class BucketSort 
{  
 public void bucketSort(float[] arr, int n)
 {     
if (n <= 0)       return;  
   @SuppressWarnings("unchecked")   
  ArrayList<Float>[] bucket = new ArrayList[n];       // Create empty buckets   
  for (int i = 0; i < n; i++)   
    bucket[i] = new ArrayList<Float>();       // Add elements into the buckets     
for (int i = 0; i < n; i++)
 {     
  int bucketIndex = (int) arr[i] * n;  
     bucket[bucketIndex].add(arr[i]);  
   }  
     // Sort the elements of each bucket     for (int i = 0; i < n; i++) 
{    
   Collections.sort((bucket[i]));   
  }    
   // Get the sorted array     int index = 0;    
 for (int i = 0; i < n; i++) 
{   
    for (int j = 0, size = bucket[i].size(); j < size; j++) 
{      
   arr[index++] = bucket[i].get(j); 
      }    
 }  
 }  
   // Driver code   public static void main(String[] args) 
{   
  BucketSort b = new BucketSort();   
  float[] arr = { (float) 0.42, (float) 0.32, (float) 0.33, (float) 0.52, (float) 0.37, (float) 0.47,         (float) 0.51 };   
  b.bucketSort(arr, 7); 
      for (float i : arr)       System.out.print(i + "  "); 
  } 
}

Related Topics

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally,...

4 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Insertion in B+ Tree

We will learn how to insert a node in the B+ tree and what are the different properties we are going to follow. Except for the root node, every node should...

5 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it...

4 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

What is a Tree in Terms of a Graph?

To know the explanation of trees in terms of graphs, we need first to know what trees and graphs are. So let us first learn about trees and graphs. Trees and...

6 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.