forked from supershivam13/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting Sort.cpp
More file actions
36 lines (34 loc) · 860 Bytes
/
Counting Sort.cpp
File metadata and controls
36 lines (34 loc) · 860 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
#include <iostream>
using namespace std;
void Countsort(int a[], int n)
{
int k = 0; //max element
for (int i = 0; i < n; i++)
if (a[i] > k)
k = a[i];
int count[k+1] = {0}, temp[n];
for (int i = 0; i < n; i++)
count[a[i]]++;
for (int i = 1; i <= k; i++)
count[i] = count[i - 1] + count[i];
for (int i = n - 1, j = k - 1; i >= 0; i--)
{
temp[count[a[i]]-1] = a[i];
count[a[i]]--;
}
for (int i = 0; i < n; i++)
a[i] = temp[i];
}
int main()
{
int n;
cout << "\nEnter No of elements in the array : ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)//input
cin >> arr[i];
Countsort(arr, n);
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++)//output
cout << arr[i] << " ";
}