-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathRadix_Sort.c
More file actions
74 lines (55 loc) · 1.13 KB
/
Radix_Sort.c
File metadata and controls
74 lines (55 loc) · 1.13 KB
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
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#define range 10
int getmax(int a[],int n)
{
int i;
int mx = a[0];
for( i = 1; i < n; i++)
if( a[i] > mx )
mx = a[i];
return mx;
}
void countsort(int arr[],int n,int place)
{
int output[n];
int i, freq[range]={0};
for( i = 0 ; i < n; i++)
freq[ (arr[i]/place)%range ]++;
for(i= 1; i < range; i++)
freq[i] += freq[i-1];
for( i = n-1; i >= 0; i--)
{
output[ freq[ (arr[i]/place)%range ] - 1 ] = arr[i];
freq[ (arr[i]/place)%range ]--;
}
for( i = 0; i < n; i++)
arr[i]=output[i];
return ;
}
void radixsort(int arr[],int n,int maxx)
{
int mul=1;
while(maxx)
{
countsort( arr, n, mul);
mul *= 10;
maxx /= 10;
}
return ;
}
int main()
{
int n,i;
printf("Enter the size of the array : ");
scanf("%d", &n);
int a[n];
printf("\nEnter the values into the array : ");
for( i = 0; i < n; ++i)
scanf("%d",&a[i]);
radixsort(a,n,getmax(a,n));
printf("\nThe Sorted Array :\n");
for( i = 0; i < n; i++)
printf("%d ",a[i]);
return 0;
}