From d994eb3a0d3a75cebaf077cd47a285d8c8c55e78 Mon Sep 17 00:00:00 2001 From: techgirl0110 <116191142+techgirl0110@users.noreply.github.com> Date: Wed, 26 Oct 2022 10:03:17 +0530 Subject: [PATCH] Added a file of bucket sort Kindly review and add the pr --- Bucketsort.java | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Bucketsort.java diff --git a/Bucketsort.java b/Bucketsort.java new file mode 100644 index 0000000..64e65b7 --- /dev/null +++ b/Bucketsort.java @@ -0,0 +1,50 @@ +import java.util.*; +public class BucketSortExample1 +{ +//user-defined method to sort array +private static void binSort(int[] array, int bucketSize) +{ +//creating a list of buckets for storing lists +List[] buckets = new List[bktSize]; +// Linked list with each bucket array index +// as there may be hash collision +for(int i = 0; i < bktSize; i++) +{ +buckets[i] = new LinkedList<>(); +} +//calculate the hash and assigns elements to the proper bucket +for(int num : array) +{ +buckets[hash(num, bktSize)].add(num); +} +//iterate over the buckets and sorts the elements +for(List bucket : buckets) +{ +//sorts the bucket +Collections.sort(bucket); +} +int index = 0; +//gethered the buckets after sorting +for(List bucket : buckets) +{ +for(int num : bucket) +{ +array[index++] = num; +} +} +} +//distributing elements +private static int hash(int num, int bucketSize) +{ +return num/bucketSize; +} +public static void main(String args[]) +{ +//array to be sort +int[] array = {22, 45, 12, 8, 10, 6, 72, 81, 33, 18, 50, 14, 55, 0, 12, 55}; +System.out.println("Unsorted Array: " + Arrays.toString(array)); +//calling the user-defined method to sort the array +binSort(array, 10); +System.out.println("Sorted Array: " + Arrays.toString(array)); +} +}