Java Program to Implement Bucket Sort

This is a java program to sort the numbers using the Bucket Sort Technique. The algorithm allocates the number of memory locations equal to maximum number and initializes all to zero, then each location is incremented as the numbers appears. The time complexity of the algorithm is O(n).

Here is the source code of the Java Program to Implement Bucket Sort. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to sort numbers using bucket sort
import java.util.Random;
 
public class Bucket_Sort 
{
    static int[] sort(int[] sequence, int maxValue) 
    {
        // Bucket Sort
        int[] Bucket = new int[maxValue + 1];
        int[] sorted_sequence = new int[sequence.length];
 
        for (int i = 0; i < sequence.length; i++)
            Bucket[sequence[i]]++;
 
        int outPos = 0;
        for (int i = 0; i < Bucket.length; i++)
            for (int j = 0; j < Bucket[i]; j++)
                sorted_sequence[outPos++] = i;
 
        return sorted_sequence;
    }
 
    static void printSequence(int[] sorted_sequence) 
    {
        for (int i = 0; i < sorted_sequence.length; i++)
            System.out.print(sorted_sequence[i] + " ");
    }
 
    static int maxValue(int[] sequence) 
    {
        int maxValue = 0;
        for (int i = 0; i < sequence.length; i++)
            if (sequence[i] > maxValue)
                maxValue = sequence[i];
        return maxValue;
    }
 
    public static void main(String args[]) 
    {
        System.out
                .println("Sorting of randomly generated numbers using BUCKET SORT");
        Random random = new Random();
        int N = 20;
        int[] sequence = new int[N];
 
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
 
        int maxValue = maxValue(sequence);
 
        System.out.println("\nOriginal Sequence: ");
        printSequence(sequence);
 
        System.out.println("\nSorted Sequence: ");
        printSequence(sort(sequence, maxValue));
    }
}

Output:

$ javac Bucket_Sort.java
$ java Bucket_Sort
 
Sorting of randomly generated numbers using BUCKET SORT
 
Original Sequence: 
95 9 95 87 8 81 18 54 57 53 92 15 38 24 8 56 29 69 64 66 
Sorted Sequence: 
8 8 9 15 18 24 29 38 53 54 56 57 64 66 69 81 87 92 95 95

Related posts:

Java Program to Implement HashTable API
Handling Errors in Spring WebFlux
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Implement Best-First Search
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java – Reader to Byte Array
Quick Intro to Spring Cloud Configuration
Check If a String Is Numeric in Java
An Intro to Spring Cloud Task
Getting a File’s Mime Type in Java
HandlerAdapters in Spring MVC
Guide to java.util.concurrent.Locks
Hướng dẫn Java Design Pattern – Service Locator
Intro to Inversion of Control and Dependency Injection with Spring
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Encode a String to UTF-8 in Java
So sánh HashMap và HashSet trong Java
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
How to Set TLS Version in Apache HttpClient
Java Program to Generate Random Hexadecimal Byte
Disable DNS caching
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Implement AttributeList API
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
The DAO with Spring and Hibernate
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Queue using Linked List
Spring Data JPA @Query
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Setting Up Swagger 2 with a Spring REST API
Java Program to Perform Complex Number Multiplication
Java Program to Implement Hash Tables Chaining with List Heads