Skip to content

Commit e9447ad

Browse files
committed
Counting sort README file added
1 parent 7811f43 commit e9447ad

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-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/)
60.1 KB
Loading

0 commit comments

Comments
 (0)