forked from dharmanshu1921/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bucketsort.java
50 lines (50 loc) · 1.31 KB
/
Bucketsort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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<Integer>[] 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<Integer> bucket : buckets)
{
//sorts the bucket
Collections.sort(bucket);
}
int index = 0;
//gethered the buckets after sorting
for(List<Integer> 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));
}
}