-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCounting.cpp
66 lines (62 loc) · 1.19 KB
/
Counting.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
using namespace std;
int k=0;
/*Method to sort the array*/
void Counting_Sort(int A[],int B[],int n)
{
int C[k];
for(int i=0;i<k+1;i++)
{
/*It will initialize the C with zero*/
C[i]=0;
}
for(int j=1;j<=n;j++)
{
/*It will count the occurence of every element x in A
and increment it at position x in C*/
C[A[j]]++;
}
for(int i=1;i<=k;i++)
{
/*It will store the last
occurence of the element i */
C[i]+=C[i-1];
}
for(int j=n;j>=1;j--)
{
/*It will place the elements at their
respective index*/
B[C[A[j]]]=A[j];
/*It will help if an element occurs
more than one time*/
C[A[j]]=C[A[j]]-1;
}
}
int main()
{
int n;
cout<<"Enter the size of the array :";
cin>>n;
/*A stores the elements input by user */
/*B stores the sorted sequence of elements*/
int A[n],B[n];
for(int i=1;i<=n;i++)
{
cin>>A[i];
if(A[i]>k)
{
/*It will modify k if an element
occurs whose value is greater than k*/
k=A[i];
}
}
Counting_Sort(A,B,n);
/*It will print the sorted sequence on the
console*/
for(int i=1;i<=n;i++)
{
cout<<B[i]<<" ";
}
cout<<endl;
return 0;
}