Skip to content

Commit 40c3b6d

Browse files
authored
Merge pull request deutranium#88 from pandeyxamit/bucket-sort
Added Readme File for Bucket Sort
2 parents e8b238f + fe4ac5d commit 40c3b6d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

sortingAlgo/bucketsort/Readme.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
## Bucket Sort
3+
Bucket Sort is a sorting technique that sorts the elements by first dividing the elements into several groups called **buckets**. The elements inside each **bucket** are sorted using any of the suitable sorting algorithms or recursively calling the same algorithm.
4+
5+
The process of bucket sort can be understood as **a scatter-gather** approach. The elements are first scattered into buckets then the elements of buckets are sorted. Finally, the elements are gathered in order.
6+
7+
bucketSort()
8+
create N buckets each of which can hold a range of values
9+
for all the buckets
10+
initialize each bucket with 0 values
11+
for all the buckets
12+
put elements into buckets matching the range
13+
for all the buckets
14+
sort elements in each bucket
15+
gather elements from each bucket
16+
end bucketSort
17+
18+
## Complexity
19+
Shell sort is an unstable sorting algorithm because this algorithm does not examine the elements lying in between the intervals.
20+
### Time Complexities:
21+
* #### Worst Case Complexity: O(n<sup>2</sup>)
22+
When there are elements of close range in the array, they are likely to be placed in the same bucket. This may result in some buckets having more number of elements than others.
23+
It makes the complexity depend on the sorting algorithm used to sort the elements of the bucket.
24+
The complexity becomes even worse when the elements are in reverse order.
25+
* #### Best Case Complexities: O(n+k)
26+
It occurs when the elements are uniformly distributed in the buckets with a nearly equal number of elements in each bucket.
27+
The complexity becomes even better if the elements inside the buckets are already sorted.
28+
* #### Average Case Complexity: O(n)
29+
It occurs when the elements are distributed randomly in the array. Even if the elements are not distributed uniformly, bucket sort runs in linear time.
30+
31+
### Bucket Sort Applications:
32+
Bucket sort is used when
33+
1. Input is uniformly distributed over a range.
34+
2. There are floating point values.
35+
36+
### Instruction for Running code:
37+
- C
38+
```
39+
gcc BucketSort.c
40+
./a.out
41+
```
42+
- Cpp
43+
44+
````
45+
g++ BucketSort.cpp
46+
./a.out
47+
````
48+
- Java
49+
50+
```
51+
javac BucketSort.java
52+
java BucketSort.class
53+
```
54+
- Python
55+
```
56+
python3 BucketSort.py
57+
```
58+

0 commit comments

Comments
 (0)