-
Notifications
You must be signed in to change notification settings - Fork 0
/
43.Count_Sort.c
66 lines (55 loc) · 1.4 KB
/
43.Count_Sort.c
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
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
// Printing Array
void printArray(int *A, int n){
for(int i=0; i < n; i++){
printf("%d ", A[i]);
}
printf("\n");
}
// Finding max number in given array
int max_number(int A[], int n){
int maxNum = INT_MIN;
for(int i=0; i < n; i++){
if(maxNum < A[i])
maxNum = A[i];
}
return maxNum;
}
void countSort(int *A, int n){
int max = max_number(A, n);
int i, j;
int * count = (int *)malloc((max+1) * sizeof(int)); // Dynamic allocation of memory for count array
// initialize all element in count memory to 0
/*for(i=0; i <= max+1; i++){
count[i] =0;
}*/
memset(count, 0, (max+1) * sizeof(int));
//Increment the corresponding index in the count array
for(i=0; i < n; i++){
count[A[i]] += 1;
}
i = 0; // using index for count array
j = 0; // using index for given A array
// Copy index numbers of count array as Value in given array A
while(i <= max){
if(count[i] > 0){
A[j] = i;
count[i] -= 1;
j++;
}
else
i++;
}
}
int main(){
int arr[] = {6, 3, 1, 4, 9, 2, 5};
int n = sizeof(arr)/sizeof(int);
printf("\nArray before sorting : ");
printArray(arr, n);
countSort(arr, n);
printf("\nArray after sorting : ");
printArray(arr, n);
return 0;
}