Skip to content

Commit c9ec4d9

Browse files
committed
Counting sort Code, README file added
1 parent bbe578f commit c9ec4d9

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Sorting/Counting Sort/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Counting Sort
2+
3+
In **Counting sort**, the frequencies of distinct elements of the array to be sorted is counted and stored in an auxiliary array, by mapping its value as an index of the auxiliary array.
4+
5+
![Counting Sort](./images/counting-sort.jpg)
6+
7+
#### Idea:
8+
9+
Let's assume that, array **A** of size **N** needs to be sorted.
10+
11+
- Initialize the auxiliary array **Aux[]** with **0**.
12+
Note: The size of this array should be greater than the Max value of the elements in **A[]**.
13+
- Traverse array **A** and store the count of occurrence of each element in the appropriate index of the **Aux** array, which means, execute **Aux[A[i]]++** for each **i**, where **i** ranges from **[0,N−1]**.
14+
- Initialize the empty array **sortedA[]**
15+
- Traverse array **Aux** and copy **i** into **sortedA** for **Aux[i]** number of times where **0≤i≤max(A[])**.
16+
17+
Simply speaking, you just need to count the frequency of each of the elements in the given array **A** and based on that frequency count write those elements in a new array, so that we would finally have an array with the sorted elements.
18+
19+
#### Video Tutorial
20+
<a href="http://www.youtube.com/watch?feature=player_embedded&v=TTnvXY82dtM" target="_blank"><img src="http://img.youtube.com/vi/TTnvXY82dtM/0.jpg"
21+
alt="Counting Sort (youtube)" width="640" height="480" border="10" /></a>
22+
23+
24+
#### Complexity Analysis
25+
Time Complexity: O(n+k), where **n** is the size of the input array and **k** is the Max value of the elements in the given Array.
26+
27+
### More on this topic
28+
- [Counting Sort - WikiPedia](https://en.wikipedia.org/wiki/Counting_sort)
29+
- [Counting Sort - HackerEarth Tutorial](https://www.hackerearth.com/practice/algorithms/sorting/counting-sort/tutorial/)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Couting Sort implementation in JavaScript */
2+
function countingSort(arr) {
3+
var i,
4+
min = 0,
5+
index = 0,
6+
aux = [],
7+
sortedArray = [];
8+
9+
var maxVal = Math.max.apply(null, arr); //If the max value of the array given, this line can be removed
10+
11+
for (i = min; i <= maxVal; i++) { //initializing the auxiliary array with 0
12+
aux[i] = 0;
13+
}
14+
15+
for (i = 0; i < arr.length; i++) { //Counting the frequency of the elements
16+
aux[arr[i]]++;
17+
}
18+
19+
for (i = min; i <= maxVal; i++) { //based on the frequency, building the new sorted array
20+
while (aux[i]-- > 0) {
21+
sortedArray[index++] = i;
22+
}
23+
}
24+
25+
return sortedArray;
26+
}
27+
28+
/******************* Testing Selection sort algorithm *********************/
29+
30+
/**
31+
* Returns a random integer between min (inclusive) and max (inclusive)
32+
* Using Math.round() will give you a non-uniform distribution!
33+
*/
34+
function getRandomInt(min, max) {
35+
return Math.floor(Math.random() * (max - min + 1)) + min;
36+
}
37+
38+
var arr = [];
39+
40+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
41+
arr.push(getRandomInt(1, 100));
42+
}
43+
44+
console.log("Unsorted array: ");
45+
console.log(arr); //printing unsorted array
46+
47+
arr = countingSort(arr);
48+
console.log("Sorted array: ");
49+
console.log(arr); //printing sorted array
60.1 KB
Loading

0 commit comments

Comments
 (0)