-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingRadix.c
More file actions
43 lines (40 loc) · 993 Bytes
/
sortingRadix.c
File metadata and controls
43 lines (40 loc) · 993 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
int maxElement(int arr[], int n) {
int max=INT_MIN;
for(int i=0; i<n; i++) {
if(arr[i]>max) max=arr[i];
}
return max;
}
void radixSort(int arr[], int n) {
int max=maxElement(arr, n);
for(int exp=1; (max/exp)>0; exp*=10) {
int *temp=(int*)calloc(sizeof(int), n);
int bucket[10]={0};
for(int i=0; i<n; i++) {
bucket[(arr[i]/exp)%10]++;
}
for(int i=1; i<10; i++) {
bucket[i]+=bucket[i-1];
}
for(int i=n-1; i>=0; i--) {
int digit=(arr[i]/exp)%10;
temp[bucket[digit]-1]=arr[i];
bucket[digit]--;
}
for(int i=0; i<n; i++) {
arr[i]=temp[i];
}
free(temp);
}
}
void print(int arr[], int n) {
for(int i=0; i<n; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[]={170, 45, 75, 90, 802, 24, 2, 66};
int n=sizeof(arr)/sizeof(arr[0]);
radixSort(arr, n);
print(arr, n);
}