Skip to content

Commit ec44c05

Browse files
committed
corrected issues
1 parent 78dd867 commit ec44c05

File tree

1 file changed

+68
-60
lines changed

1 file changed

+68
-60
lines changed

Algorithms/Sorting/BucketSort/readme.md

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Bucket Sort
2+
23
Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm or by recursively applying the bucket sort algorithm. After all the individual buckets are sorted, the elements are concatenated together to form the sorted array.
34

45
## Complexity
6+
57
| Best | Average | Worst | Memory | Stable |
68
|------|---------|-------|--------|--------|
79
| log(n+k) | log(n+k) | log(n^2) | n | Yes |
810

9-
1011
## Pseudo Code
12+
1113
```
1214
BucketSort(arr, n):
1315
# Find the minimum and maximum values in the array
@@ -38,16 +40,16 @@ BucketSort(arr, n):
3840
```
3941

4042
## Implementations
41-
* [Python](#python)!
42-
* [C++](#cpp)!
43-
* [C](#c)!
44-
* [Java](#java)!
45-
* [JavaScript](#javascript)
46-
* [Go](#go)!
47-
* [Ruby](#ruby)!
4843

44+
* [Python](#python)
45+
* [C++](#cpp)
46+
* [C](#c)
47+
* [Java](#java)
48+
* [JavaScript](#javascript)
49+
* [Go](#go)
4950

5051
### Python
52+
5153
```python
5254
def bucket_sort(arr):
5355
# Find the minimum and maximum values in the input array
@@ -81,6 +83,7 @@ print(sorted_arr)
8183
```
8284

8385
### CPP
86+
8487
```cpp
8588
#include <iostream>
8689
#include <vector>
@@ -128,7 +131,9 @@ int main() {
128131
}
129132

130133
```
134+
131135
### C
136+
132137
```c
133138
#include <stdio.h>
134139
#include <stdlib.h>
@@ -193,7 +198,7 @@ void bucketSort(double arr[], int size) {
193198
buckets[i] = NULL;
194199
}
195200
// Place each element in the appropriate bucket
196-
for (int i = 0; i < size; i++) {
201+
for (int i = 0; i < numBuckets + 1; i++) {
197202
int index = (int)((arr[i] - minVal) / bucketSize);
198203
insert(&buckets[index], arr[i]);
199204
}
@@ -222,6 +227,7 @@ int main() {
222227
```
223228

224229
### Java
230+
225231
```java
226232
import java.util.ArrayList;
227233
import java.util.Collections;
@@ -271,6 +277,7 @@ public class BucketSort {
271277
```
272278

273279
### JavaScript
280+
274281
```javascript
275282
function bucketSort(arr) {
276283
// Find the minimum and maximum values in the array
@@ -338,66 +345,67 @@ console.log(sortedArray);
338345
```
339346

340347
### Go
348+
341349
```go
342350
package main
343351

344352
import (
345-
"fmt"
346-
"sort"
353+
"fmt"
354+
"sort"
347355
)
348356

349357
func bucketSort(arr []float64) []float64 {
350-
if len(arr) == 0 {
351-
return arr
352-
}
353-
354-
// Find the minimum and maximum values in the array
355-
minVal, maxVal := arr[0], arr[0]
356-
for _, num := range arr {
357-
if num < minVal {
358-
minVal = num
359-
}
360-
if num > maxVal {
361-
maxVal = num
362-
}
363-
}
364-
365-
// Determine the range and the number of buckets
366-
rangeVal := maxVal - minVal
367-
numBuckets := len(arr)
368-
bucketSize := rangeVal / float64(numBuckets)
369-
370-
// Create an array of empty buckets
371-
buckets := make([][]float64, numBuckets)
372-
for i := range buckets {
373-
buckets[i] = make([]float64, 0)
374-
}
375-
376-
// Place each element in the appropriate bucket
377-
for _, num := range arr {
378-
index := int((num - minVal) / bucketSize)
379-
380-
// Ensure that the index remains within a valid range
381-
if index == numBuckets {
382-
index--
383-
}
384-
buckets[index] = append(buckets[index], num)
385-
}
386-
387-
// Sort each bucket and concatenate them to get the sorted array
388-
sortedArr := make([]float64, 0, len(arr))
389-
for _, bucket := range buckets {
390-
sort.Float64s(bucket)
391-
sortedArr = append(sortedArr, bucket...)
392-
}
393-
394-
return sortedArr
358+
if len(arr) == 0 {
359+
return arr
360+
}
361+
362+
// Find the minimum and maximum values in the array
363+
minVal, maxVal := arr[0], arr[0]
364+
for _, num := range arr {
365+
if num < minVal {
366+
minVal = num
367+
}
368+
if num > maxVal {
369+
maxVal = num
370+
}
371+
}
372+
373+
// Determine the range and the number of buckets
374+
rangeVal := maxVal - minVal
375+
numBuckets := len(arr)
376+
bucketSize := rangeVal / float64(numBuckets)
377+
378+
// Create an array of empty buckets
379+
buckets := make([][]float64, numBuckets)
380+
for i := range buckets {
381+
buckets[i] = make([]float64, 0)
382+
}
383+
384+
// Place each element in the appropriate bucket
385+
for _, num := range arr {
386+
index := int((num - minVal) / bucketSize)
387+
388+
// Ensure that the index remains within a valid range
389+
if index == numBuckets {
390+
index--
391+
}
392+
buckets[index] = append(buckets[index], num)
393+
}
394+
395+
// Sort each bucket and concatenate them to get the sorted array
396+
sortedArr := make([]float64, 0, len(arr))
397+
for _, bucket := range buckets {
398+
sort.Float64s(bucket)
399+
sortedArr = append(sortedArr, bucket...)
400+
}
401+
402+
return sortedArr
395403
}
396404

397405
func main() {
398-
arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9}
399-
sortedArr := bucketSort(arr)
400-
fmt.Println(sortedArr)
406+
arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9}
407+
sortedArr := bucketSort(arr)
408+
fmt.Println(sortedArr)
401409
}
402410

403-
```
411+
```

0 commit comments

Comments
 (0)