Skip to content

Commit 5dc7195

Browse files
Paawan Mukkersangamcse
authored andcommitted
counting_sort.cpp: Add Counting Sort Algorithm
This adds Counting sort which sorts array by counting the occurences of elements. Closes #299
1 parent ecdd0d3 commit 5dc7195

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This repository contains examples of various algorithms written on different pro
1616
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
1717
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) |
1818
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
19-
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) |
19+
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
2020
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) |
2121
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
2222
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |

counting_sort/Cpp/counting_sort.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <cmath>
4+
using namespace std;
5+
6+
/*
7+
Counting sort implementation for sorting given array.
8+
Note - Works on Non-negative integer array only.
9+
10+
Parameters:
11+
int a[] - Integer array a to be sorted.
12+
int n - size of array a.
13+
int r. - Range of numbers in a.
14+
*/
15+
16+
void counting_sort(int a[], int n, int r) {
17+
if(a == NULL) return;
18+
19+
int *cnt = new int[r+1]; //array to store count of each occurences
20+
int *out = new int[n]; //Out array to store sorted values
21+
22+
23+
// Intitialise the count array to 0
24+
for (int i = 0; i <= r; ++i)
25+
cnt[i] = 0;
26+
27+
// Count each occurences digit wise
28+
for (int i = 0; i < n; ++i)
29+
cnt[a[i]]++;
30+
31+
// Cumulative count array
32+
for (int i = 1; i <= r; ++i)
33+
cnt[i]+=cnt[i-1];
34+
35+
// Sort
36+
for (int i = n - 1; i >= 0; i--) {
37+
// Note - starting from n-1 to maintain "stable sort" property
38+
out[cnt[a[i]] - 1] = a[i];
39+
cnt[a[i]]--;
40+
}
41+
42+
// Copy results back to orignal array
43+
for (int i = 0; i < n; i++)
44+
a[i] = out[i];
45+
}
46+
47+
48+
int main() {
49+
ios_base::sync_with_stdio(false); //For faster io
50+
int a[] = {1, 2, 9, 293, 41, 15, 52, 3, 121, 7, 1223, 3449, 15, 1 };
51+
int n = sizeof(a)/sizeof(a[0]);
52+
53+
// Get the iterator to max element for calculating range of number
54+
int * max_a_itr = max_element(a, a+n);
55+
int max_a = *max_a_itr;
56+
57+
58+
cout << "Input Array:\n";
59+
for (int i = 0; i < n; i++) {
60+
cout << a[i] << " ";
61+
}
62+
cout << "\n";
63+
64+
counting_sort(a, n, max_a);
65+
66+
cout << "Sorted Array:\n";
67+
for (int i = 0; i < n; i++) {
68+
cout << a[i] << " ";
69+
}
70+
cout << "\n";
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)