-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.cpp
More file actions
48 lines (48 loc) · 980 Bytes
/
CountSort.cpp
File metadata and controls
48 lines (48 loc) · 980 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
45
46
47
48
#include <iostream>
#include <vector>
using namespace std;
void CountSort(vector<int> arr, int n)
{
int max_ele = arr[0];
for (int i = 0; i < n; i++)
{
max_ele = max(max_ele, arr[i]);
}
vector<int> freq(max_ele, 0);
for (int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
for (int i = 1; i <= max_ele; i++)
{
freq[i] += freq[i - 1];
}
vector<int> output(n, 0);
for (int i = n - 1; i >= 0; i--)
{
output[--freq[arr[i]]] = arr[i];
}
for (int i = 0; i < n; i++)
{
arr[i] = output[i];
}
for (auto x : arr)
cout << x << " ";
}
int main()
{
vector<int> arr;
int n, a, i;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
{
cin >> a;
arr.push_back(a);
}
CountSort(arr, n);
return 0;
}
// Time Complexity=O(n+range)
// Space Complexity=O(n+range)