-
Notifications
You must be signed in to change notification settings - Fork 69
/
Radix_Sort.c
55 lines (55 loc) · 970 Bytes
/
Radix_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
#include<stdio.h>
void countsort(int a[],int n,int i)
{
int count[10]={0},b[30],j;
for(j=0;j<n;j++)
{
++count[(a[j]/i)%10];
}
for(j=1;j<=9;j++)
{
count[j]=count[j]+ count[j-1];
}
for(j=n-1;j>=0;j--)
{
b[--count[(a[j]/i)%10]]=a[j];
}
for(j=0;j<n;j++)
{
a[j]=b[j];
}
}
int getmax(int a[],int n)
{
int i=0;
int max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
return max;
}
void radixsort(int a[],int n)
{
int max=getmax(a,n),i;
for(i=1;max/i>0;i=i*10)
{
countsort(a,n,i);
}
}
int main()
{
int a[30],count[10]={0},b[30],n,i;
printf("\nEnter number of elements of array:\n");
scanf("%d",&n);
printf("\nEnter elements of array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
radixsort(a,n);
printf("\nArray after Sorting:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}