-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcountsort.cpp
79 lines (74 loc) · 1.08 KB
/
countsort.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
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
using namespace std;
void show(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl<<"-------------------------"<<endl;
}
void in(int a[],int n){
cout<<"Enter elements:"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
}
int countsort(int a[],int n,int o){
int i,k,j,pass=0;
int b[n];
int c[o+1];
for(i=0;i<=o;i++){
c[i]=0;
pass+=1;
}
for(i=0;i<n;i++){
pass++;
c[a[i]]+=1;
}
for(i=0;i<n;i++){
b[i]=0;
}
cout<<"Count array "<<endl;
show(c,o+1);
for(i=1;i<=o;i++){
c[i]+=c[i-1];
pass+=1;
}
cout<<"Count array "<<endl;
show(c,o+1);
for(j=n-1;j>=0;j--){
i=c[a[j]]-1;
b[i]=a[j];
c[a[j]]-=1;
pass++;
cout<<"array c ";
show(c,o+1);
cout<<"array B ";
show(b,n);
}
cout<<"output array "<<endl;
show(b,n);
for(i=0;i<n;i++){
a[i]=b[i];
}
return pass;
}
int max(int a[],int n){
int t=a[0];
for(int i=1;i<n;i++)
{
if(t<a[i])
t=a[i];
}
return t;
}
int main(void){
int n;
cout<<"Enter size ";
cin>>n;
int a[n];
in(a,n);
int y;
y=countsort(a,n,max(a,n));
show(a,n);
cout<<"Pass no "<<y;
return 0;
}