Skip to content

Commit 3d84df9

Browse files
authored
Merge pull request #34 from Anirbansikder/second_branch
Added Other Sorting Algorithms (Count, Bucket, Heap)
2 parents 3f26d9f + 2879f76 commit 3d84df9

File tree

5 files changed

+417
-0
lines changed

5 files changed

+417
-0
lines changed

src/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
- [Ternary Search](./Searching_Algos/Ternary_search/Explanation.md)
1515
- [Sorting Algorithms](./SortingAlgorithms/README.md)
1616
- [Quick Sort](./SortingAlgorithms/QuickSort.md)
17+
- [Count Sort](./SortingAlgorithms/CountSort.md)
18+
- [Bucket Sort](./SortingAlgorithms/BucketSort.md)
19+
- [Heap Sort](./SortingAlgorithms/HeapSort.md)
1720
- [Radix Sort](./SortingAlgorithms/RadixSort.md)
1821
- [Insertion Sort](./SortingAlgorithms/InsertionSort/insertion.md)
1922
- [Merge Sort](./SortingAlgorithms/MergeSort/merge.md)

src/SortingAlgorithms/BucketSort.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Bucket Sort
2+
3+
## Example Problem
4+
5+
Given an array ``arr = [a1,a2,a3,a4,a5...,an] `` , we need to sort the array with the help of Bucket Sort algorithm.
6+
7+
<hr>
8+
9+
## Discussion
10+
11+
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.
12+
13+
<hr>
14+
15+
## Working of the Algorithm
16+
17+
Several buckets are created. Each bucket is filled with a specific range of elements. The elements inside the bucket are sorted using any other algorithm. Finally, the elements of the bucket are gathered to get the sorted array.
18+
19+
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.
20+
21+
![Bucket Sort](https://cdn.programiz.com/sites/tutorial2program/files/Bucket_2.png)
22+
23+
## Illustration
24+
25+
Let's Sort an array of decimal numbers in th range 0 to 1.
26+
```
27+
Input array : 0.42 0.32 0.23 0.52 0.25 0.47 0.51
28+
29+
Create an array of size 10. Each slot of this array is used as a bucket for storing elements.
30+
31+
Insert elements into the buckets from the array. The elements are inserted according to the range of the bucket. And Sort them with a suitable sorting technique.
32+
33+
Representation of the buckets ->
34+
35+
| | | 0.23 | | 0.42 | 0.51 | | | | |
36+
| 0 | 0 | 0.25 | 0.32 | 0.47 | 0.52 | 0 | 0 | 0 | 0 |
37+
38+
0 1 2 3 4 5 6 7 8 9
39+
40+
Traverse From Left to right and put the elements back into array in the same order.
41+
42+
Sorted Array : 0.23 0.25 0.32 0.42 0.47 0.51 0.52
43+
```
44+
45+
<hr>
46+
47+
## Code
48+
49+
### Code For Bucket Sort.
50+
```cpp
51+
void BucketSort(double a[],int n)
52+
{
53+
// creating vector array of size 10
54+
vector <double> bucket[10];
55+
56+
// putting elements in respective bucket
57+
for(int i=0;i<n;i++)
58+
bucket[ int(10 * a[i]) ].push_back(a[i]);
59+
60+
// sorting each bucket with quick sort
61+
for(int i=0;i<10;i++)
62+
sort(bucket[i].begin(),bucket[i].end());
63+
64+
// picking from bucket and placing back to the array
65+
for(int i=0, j=0;i<10;i++)
66+
{
67+
for(auto x : bucket[i])
68+
{
69+
a[j] = x;
70+
j++;
71+
}
72+
}
73+
74+
// clearing the each bucket
75+
for(int i=0;i<10;i++)
76+
bucket[i].clear();
77+
}
78+
```
79+
<hr>
80+
81+
## Time Complexity
82+
83+
**Worst Case Complexity** : O ( n<sup>2</sup> )
84+
85+
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.
86+
It makes the complexity depend on the sorting algorithm used to sort the elements of the bucket.
87+
88+
The complexity becomes even worse when the elements are in reverse order. If insertion sort is used to sort elements of the bucket, then the time complexity becomes O ( n<sup>2</sup> ).
89+
90+
**Best Case Complexity**: O(n+k)
91+
92+
It occurs when the elements are uniformly distributed in the buckets with a nearly equal number of elements in each bucket.
93+
The complexity becomes even better if the elements inside the buckets are already sorted.
94+
95+
If insertion sort is used to sort elements of a bucket then the overall complexity in the best case will be linear 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 bucket using algorithms having linear time complexity at the best case.
96+
97+
<hr>
98+
99+
Other Resources : [GFG Blog](https://www.geeksforgeeks.org/bucket-sort-2/)

src/SortingAlgorithms/CountSort.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Count Sort
2+
3+
## Example Problem
4+
5+
Given an array `arr = [a1,a2,a3,a4,a5...,an] ` , we need to sort the array with the help of Count Sort algorithm.
6+
7+
<hr>
8+
9+
## Discussion
10+
11+
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values. Then doing some arithmetic to calculate the position of each object in the output sequence.
12+
13+
Take a count array of size ` max_element + 1` and initialise the array with 0 . Now iterate through the initial array and for each element x increase the value of count array at index x by 1.
14+
15+
But this method won't work for negative integers .
16+
17+
So we take a count array of size `max_element - min_element + 1` and initialize with 0. Now iterate through the initial array and for each element x increase the value of count array at index `x - min_element` by 1.
18+
19+
Now Iterate through the count array and if value at the `index + min_element` is greater than 0 keep on adding the index value to the array , and decrement the value of the index by 1.
20+
21+
<hr>
22+
23+
## Working of the Algorithm
24+
25+
```
26+
Consider array = [1 , 3 , -5 , 3 , 4 , -2 , 0]
27+
28+
Here max_element = -5 and min_element = 4
29+
30+
So, Create an array of size (4 - (-5) + 1) = 10 and initialise with 0.
31+
32+
count_array = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
33+
idx_values-> 0 1 2 3 4 5 6 7 8 9
34+
35+
After the iteration , count array becomes ->
36+
37+
count_array = [ 1 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 2 , 1 ]
38+
idx_values-> 0 1 2 3 4 5 6 7 8 9
39+
40+
The Sorted Array becomes : [-5 , -2 , 0 , 1 , 3 , 3 , 4]
41+
```
42+
43+
<hr>
44+
45+
## Code
46+
47+
### Code For Count Sort
48+
49+
```cpp
50+
void CountSort(int a[],int n)
51+
{
52+
int max_element = -INT_MAX , min_element = INT_MAX ;
53+
54+
// for finding the max_element in the array
55+
for(int i=0;i<n;i++)
56+
max_element = max(max_element , a[i]);
57+
58+
// for finding the min_element in the array
59+
for(int i=0;i<n;i++)
60+
min_element = min(min_element , a[i]);
61+
62+
// initisalizing a count vector of size max_element - min_element + 1
63+
vector <int> count(max_element - min_element +1 , 0);
64+
65+
// setting count vector according to the algorithm
66+
for(int i=0;i<n;i++)
67+
count[a[i] - min_element]++;
68+
69+
// updating the actual array
70+
int j = 0,i=0;
71+
while(i<max_element - min_element +1)
72+
{
73+
if(count[i] == 0)
74+
i++;
75+
else
76+
{
77+
a[j] = min_element + i;
78+
count[i]--;
79+
j++;
80+
}
81+
82+
}
83+
}
84+
```
85+
86+
<hr>
87+
88+
## Time Complexity
89+
90+
**Time Complexity**: O(n+k) where n is the number of elements in input array and k is the range of input.
91+
92+
**Space Complexity**: Algorithm takes extra space of O(k).
93+
94+
<hr>
95+
96+
## Important
97+
Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted. Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
98+
99+
<hr>
100+
101+
Other Resources : [GFG Blog](https://www.geeksforgeeks.org/counting-sort/)

0 commit comments

Comments
 (0)