-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountsort.cpp
More file actions
50 lines (43 loc) · 764 Bytes
/
Copy pathcountsort.cpp
File metadata and controls
50 lines (43 loc) · 764 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
49
50
#include<iostream>
using namespace std;
int findmax(int a[],int n)
{
int max=-32768;
for(int i=0;i<n;i++)
{
if(a[i]>max)
a[i]=max;
}
return max;
}
void countsort(int a[],int n)
{
int max=findmax(a,n);
int count[max+1]={0};
// int *count;
// count=(int*)malloc(sizeof(int)*(max+1));
// for(int i=0;i<max+1;i++)
// count[i]=0;
for(int i=0;i<n;i++)
count[a[i]]++;
for(int i=0,j=0;i<max+1;)
{
if(count[i]>0)
{
a[j++]=i;
count[i]--;
}
else
{
i++;
}
}
for(int i=0;i<n;i++)
printf("%d " ,a[i]) ;
}
int main()
{
int a[]={6,8,4,3,10,7,5,11,6},n=8;
countsort(a,n);
return 0;
}