-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.c
More file actions
60 lines (50 loc) · 917 Bytes
/
Copy pathCountSort.c
File metadata and controls
60 lines (50 loc) · 917 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
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
int findMax(int a[],int n){
int max=0;
int i;
for(i=0;i<n;i++){
if(a[i]>max){
max=a[i];
}
}
return max;
}
void CountSort(int a[],int n){
int i,j,max;
int *C;
max=findMax(a,n);
C=(int *)malloc(sizeof(int)*(max+1));
for(i=0;i<max+1;i++){
C[i]=0;
}
for(i=0;i<n;i++){
C[a[i]]++;
}
i=0,j=0;
while(i<max+1){
if(C[i]>0){
a[j++]=i;
C[i]--;
}
else{
i++;
}
}
printf("After sorting: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
}
int main() {
int a[1000];
int n;
printf("Enter number of components: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter element: ");
scanf("%d", &a[i]);
}
CountSort(a, n);
return 0;
}