Skip to content

Commit 652df48

Browse files
authored
Update ArrayBucketSortEx.java
1 parent 24908d8 commit 652df48

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

comparesort/ArrayBucketSortEx.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ public class ArrayBucketSortEx {
88

99
static int[] doBucketSort(int[] vals, int max) {
1010

11-
// Bucket Sort
11+
// creates empty bucket and sorted array objects
1212
int[] bucket = new int[max + 1];
1313
int[] sorted_vals = new int[vals.length];
1414

15+
// each value is an index in the bucket having value 1
16+
// if there are same values, the index is incremented
1517
for (int i = 0; i < vals.length; i++) {
1618
bucket[vals[i]]++;
1719
}
1820

1921
int outPos = 0;
2022

23+
// the bucket elements with non zero values are added
24+
// to the sorted_values array
2125
for (int i = 0; i < bucket.length; i++) {
2226
for (int j = 0; j < bucket[i]; j++) {
2327
sorted_vals[outPos++] = i;
@@ -26,7 +30,8 @@ static int[] doBucketSort(int[] vals, int max) {
2630

2731
return sorted_vals;
2832
}
29-
33+
34+
// calculates max value
3035
static int max(int[] vals) {
3136

3237
int max = 0;

0 commit comments

Comments
 (0)