-
Notifications
You must be signed in to change notification settings - Fork 127
/
counting-sort.cpp
40 lines (32 loc) · 1.02 KB
/
counting-sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Done by Sanjoy Saha
// Github user link - https://github.com/SanjoySaha24
// Counting Sort - 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 (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.
#include<bits/stdc++.h>
using namespace std;
void countSort(int arr[], int n, int k)
{
int count[k];
for(int i=0;i<k;i++)
count[i]=0;
for(int i=0;i<n;i++)
count[arr[i]]++;
for(int i=1;i<k;i++)
count[i]=count[i-1]+count[i];
int output[n];
for(int i=n-1;i>=0;i--){
output[count[arr[i]]-1]=arr[i];
count[arr[i]]--;
}
for(int i=0;i<n;i++)
arr[i]=output[i];
}
int main()
{
int arr[] = { 1,4,4,1,0,1 };
int n = sizeof(arr) / sizeof(arr[0]);
int k=5;
countSort(arr, n, k);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}