Counting Sort is an efficient algorithm for sorting a collection of objects when the range of potential items is known and not too large.
Bucket Sort:
Bucket Sort improves upon simple algorithms like Counting Sort by distributing the elements among several "buckets" or lists, which are then sorted individually, typically using a different sorting algorithm.
1functioncountingSort(arr) {
2let max = Math.max(...arr);
3let count = Array(max+1).fill(0);
4 arr.forEach(item => count[item]++);
5let output = []
6for(let i = 0; i < count.length; i++) {
7while(count[i] >0) {
8 output.push(i);
9 count[i]--
10 }
11 }
12return output
13}
141516let arr = [5, 2, 5, 5, 3, 1, 2, 5, 1, 5, 0, 5, 3, 1, 5, 2, 2, 2];
17console.log(countingSort(arr));
1819//Buckert Sort is not good in cases where data is not uniform.20//It's particularly useful when the input is uniformly distributed over a range.21let arr = [11,9,21,8,17,19,13,1,24,12,21,22]
22functionbucketSort(array, bucketSize = 5){
23if (array.length === 0) {
24return array;
25 }
26let max = Math.max(...array)
27let min = Math.min(...array)
282930const bucketCount = Math.floor((max - min)/bucketSize) + 1;
3132const buckets = newArray(bucketCount);
33for (i = 0; i < buckets.length; i++) {
34 buckets[i] = [];
35}
36for(let i = 0; i < array.length; i++) {
37 buckets[Math.floor((array[i]-min)/bucketSize)].push(array[i]);
38}
39array.length = 0;
40for (i = 0; i < buckets.length; i++) {
41 buckets[i].sort((a, b) => a - b);
42for (let j = 0; j < buckets[i].length; j++) {
43 array.push(buckets[i][j]);
44 }
45}
46return array;
47}
4849let bucketSize = 550console.log(bucketSort(arr, bucketSize))
515253//The space complexity is O(n+k)54