-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountsort.c
More file actions
52 lines (51 loc) · 931 Bytes
/
Countsort.c
File metadata and controls
52 lines (51 loc) · 931 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
51
52
#include<stdio.h>
#include<stdlib.h>
void traverse(int *a , int n){
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
int maximum(int *a, int n){
int max=a[0];
for(int i=0; i<n;i++){
if(a[i]>max){
max = a[i];
}
}
return max;
}
void countsort(int *a, int n){
int i,j;
int max = maximum(a,n);
int *count=(int*)malloc((max+1)*sizeof(int));
for ( i = 0; i < max+1; i++)
{
count[i]=0;
}
for ( i = 0; i < n; i++)
{
count[a[i]]=count[a[i]]+1;
}
i=0; j=0;
while(i<=max){
if(count[i]>0){
a[j]=i;
count[i]=count[i]-1;
j++;
}
else{
i++;
}
}
}
int main(){
int a[]= {23,45,67,54,32,99,87,91};
int n=8;
traverse(a,n);
printf("After sorting.... \n");
countsort(a,n);
traverse(a,n);
return 0;
}