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 Generate N Number of Passwords of Length M Each
Configure a RestTemplate with RestTemplateBuilder
Hướng dẫn Java Design Pattern – State
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
A Quick JUnit vs TestNG Comparison
Spring Cloud AWS – RDS
Guide to the Fork/Join Framework in Java
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement Pagoda
Debugging Reactive Streams in Java
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Check whether Directed Graph is Connected using DFS
Spring Security Remember Me
“Stream has already been operated upon or closed” Exception in Java
JUnit 5 @Test Annotation
Receive email by java client
Convert Hex to ASCII in Java
JUnit5 Programmatic Extension Registration with @RegisterExtension
A Guide to HashSet in Java
Concatenating Strings In Java
Từ khóa this và super trong Java
Java Program to Perform LU Decomposition of any Matrix
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Java Program to Implement Fermat Primality Test Algorithm
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Implement Threaded Binary Tree
Hamcrest Collections Cookbook
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Implement Nth Root Algorithm
Spring Boot - Internationalization
Runnable vs. Callable in Java