forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting_sort.c
More file actions
38 lines (38 loc) · 822 Bytes
/
Counting_sort.c
File metadata and controls
38 lines (38 loc) · 822 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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void countingSort(int* arr, int highest, int num){
int* Count = (int*)malloc(highest*sizeof(int));
int* Sorted = (int*)malloc(num*sizeof(int));
for(int i=0;i<highest+1;i++)
Count[i]=0;
for(int i=0;i<num;i++){
Count[arr[i]]++;
}
int j=0;
for(int i=0;i<=highest;i++){
int temp = Count[i];
while(temp--){
Sorted[j]=i;
j++;
}
}
printf("The sorted array is : \n");
for(int i=0;i<num;i++)
printf("%d ",Sorted[i]);
}
int main(void) {
int* arr;
printf("Enter the number of elements : ");
int n,k=0;
scanf("%d",&n);
arr = (int*)malloc(n*sizeof(int));
printf("Enter the elements : ");
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
if(arr[i]>k)
k = arr[i];
}
countingSort(arr,k,n);
return 0;
}