-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcountSort.cpp
More file actions
44 lines (44 loc) · 846 Bytes
/
Copy pathcountSort.cpp
File metadata and controls
44 lines (44 loc) · 846 Bytes
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
41
42
43
44
#include <iostream>
#include <vector>
using namespace std;
//time O(n+k) n is elemnts in array and k is the range of elements
//space O(n+k) array of size(n+k) has to be created
void counting_sort(int a[], int n)
{
//Largest Element
int largest = -1;
for (int i = 0; i < n; i++)
{
largest = max(largest, a[i]);
}
//create a count array/vector
vector<int> freq(largest + 1, 0);
//Update the freq array
for (int i = 0; i < n; i++)
{
freq[a[i]]++;
}
//Put back the elements from freq into original array
int j = 0;
for (int i = 0; i <= largest; i++)
{
while (freq[i] > 0)
{
a[j] = i;
freq[i]--;
j++;
}
}
return;
}
int main()
{
int arr[] = {88, 97, 10, 12, 15, 1, 5, 6, 12, 5, 8};
int n = sizeof(arr) / sizeof(int);
counting_sort(arr, n);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
return 0;
}