Skip to content

Commit 657e4da

Browse files
committed
Add Bucket Sort
1 parent 61163c8 commit 657e4da

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

src/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Main {
77
public static void main(String[] args) {
88
int[] arr = {5, 3, 4, 2, 1};
99

10-
SortingService.sort(arr, SortingAlgorithms.RADIX_SORT);
10+
SortingService.sort(arr, SortingAlgorithms.BUCKET_SORT);
1111

1212
System.out.println(Arrays.toString(arr));
1313
}

src/sort/SortingAlgorithms.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public enum SortingAlgorithms {
66
INSERTION_SORT,
77
CYCLE_SORT,
88
COUNTING_SORT,
9-
RADIX_SORT
9+
RADIX_SORT,
10+
BUCKET_SORT
1011
}

src/sort/SortingService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort;
22

33
import sort.bubble.BubbleSort;
4+
import sort.bucket.BucketSort;
45
import sort.counting.CountingSort;
56
import sort.cycle.CycleSort;
67
import sort.insertion.InsertionSort;
@@ -32,6 +33,9 @@ public static void sort(int[] arr, SortingAlgorithms sortAlgorithms) {
3233
case RADIX_SORT:
3334
RadixSort.sort(arr);
3435
break;
36+
case BUCKET_SORT:
37+
BucketSort.sort(arr);
38+
break;
3539
}
3640
}
3741
}

src/sort/bucket/BucketSort.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package sort.bucket;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
public class BucketSort {
7+
public static void sort(int[] arr) {
8+
System.out.println("Sorting Elements Using Bucket Sort");
9+
10+
int n = arr.length;
11+
12+
// find max element
13+
int maxElement = Integer.MIN_VALUE;
14+
for (int i = 0; i < n; i++) {
15+
if (arr[i] > maxElement)
16+
maxElement = arr[i];
17+
}
18+
// increment to compute right bucket index
19+
maxElement++;
20+
21+
// assume total buckets
22+
int totalBuckets = 5;
23+
24+
// create buckets
25+
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(totalBuckets);
26+
27+
// add ArrayList to each bucket to hold elements
28+
for (int i = 0; i < totalBuckets; i++) {
29+
buckets.add(new ArrayList<>());
30+
}
31+
32+
// move elements to their appropriate bucket
33+
for (int i = 0; i < n; i++) {
34+
int bucketIndex = (totalBuckets * arr[i]) / maxElement;
35+
ArrayList<Integer> bucket = buckets.get(bucketIndex);
36+
bucket.add(arr[i]);
37+
}
38+
39+
// sort all the buckets individually
40+
for (int i = 0; i < totalBuckets; i++) {
41+
ArrayList<Integer> bucket = buckets.get(i);
42+
Collections.sort(bucket);
43+
}
44+
45+
// concatenate elements of all the buckets
46+
int index = 0;
47+
for (int i = 0; i < totalBuckets; i++) {
48+
ArrayList<Integer> bucket = buckets.get(i);
49+
50+
for (int j = 0; j < bucket.size(); j++) {
51+
arr[index] = bucket.get(j);
52+
index++;
53+
}
54+
}
55+
}
56+
}

src/sort/bucket/README.MD

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Bucket Sort
2+
3+
*Sort elements using bucket*
4+
5+
### How It Works: [Link to Video](https://www.youtube.com/watch?v=rNdTWHQMvOk)
6+
7+
### Time Complexity
8+
9+
---
10+
11+
1. **Worst Case Complexity: O(n<sup>2</sup>)**
12+
When there are elements of close range in the array, they are likely to be placed in the same bucket. This may result
13+
in some buckets having more number of elements than others.
14+
It makes the complexity depend on the sorting algorithm used to sort the elements of the bucket.
15+
The complexity becomes even worse when the elements are in reverse order. If insertion sort is used to sort elements
16+
of the bucket, then the time complexity becomes O(n<sup>2</sup>).
17+
2. **Best Case Complexity: O(n+k)**
18+
It occurs when the elements are uniformly distributed in the buckets with a nearly equal number of elements in each
19+
bucket.
20+
The complexity becomes even better if the elements inside the buckets are already sorted.
21+
If insertion sort is used to sort elements of a bucket then the overall complexity in the best case will be linear
22+
ie. O(n+k). O(n) is the complexity for making the buckets and O(k) is the complexity for sorting the elements of the
23+
bucket using algorithms having linear time complexity at the best case.
24+
3. **Average Case Complexity: O(n)**
25+
It occurs when the elements are distributed randomly in the array. Even if the elements are not distributed
26+
uniformly, bucket sort runs in linear time. It holds true until the sum of the squares of the bucket sizes is linear
27+
in the total number of elements.
28+
29+
### Space Complexity
30+
31+
---
32+
33+
1. The space complexity of Bucket Sort is O(n+k) where k is no of buckets
34+
35+
### Properties
36+
37+
1. May or may not be Stable
38+
2. Not Inplace
39+
3. Buckets are used to sort elements
40+
41+
### Applications
42+
43+
1. Input is uniformly distributed over a range.
44+
2. There are floating point values

src/sort/radix/RadixSort.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public class RadixSort {
44
public static void sort(int[] arr) {
5+
System.out.println("Sorting Elements Using Radix Sort");
6+
57
int n = arr.length;
68

79
// find max element in the array

0 commit comments

Comments
 (0)