This repository was archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradixsort.c
More file actions
137 lines (116 loc) · 2.2 KB
/
radixsort.c
File metadata and controls
137 lines (116 loc) · 2.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<stdio.h>
#include<stdlib.h>
int pow1(int a)
{
int i=0;
int c = 1;
for(i=0;i<a;i++)
{ c = c*10;
}
return c;
}
void radixsort( int arr[], int front, int n, int d);
void bucketsort( int arr[], int front, int n, int d)
{
int c = pow1(d-1);
int b[n][10];
int i;
int j;
int k = 0;
for ( i=0; i<n; i++)
{
for(j=0;j<10;j++)
{
b[i][j] = 0;
}
}
for ( i = front; i<n; i++)
{
j = (arr[i]/c)%10;
if(j!=0)
{
if(b[0][j]==0)
{
b[0][j] = arr[i];
}
else
{ int l=0;
while(b[l][j]!=0)
{
l = l+1;
}
b[l][j] = arr[i];
}
}
else
{
b[k][0] = arr[i];
k++;
}
}
j = 0;
k=front;
i=0;
while( i <n && j<10 )
{
if(b[i][j]!=0)
{
arr[k++] = b[i][j];
i=i+1;
}
else
{
j=j+1;
i=0;
}
}
}
int m;
int main()
{
int n; int i;
char c[20][100];
scanf("%d\n",&n);
int arr[n];
for(i=0;i<n;i++)
{
gets(c[i]);
arr[i] = atoi(c[i]);
}
m = n;
radixsort(arr, 0, n, 5);
}
void radixsort( int arr[], int front, int n, int d)
{
if(n-front>=0 && d>=0)
{ int i;
bucketsort(arr, front, n, d);
for(i=0; i<m; i++)
printf(" %d",arr[i]);
printf("\n");
int k = (arr[front]/pow1(d-1))%10;
int front1 = front;
int rear1 = front;
while(rear1<n )
{
if(arr[rear1]/(pow1(d-1))== k)
{
rear1+=1;
}
else
{
radixsort(arr,front1,rear1,d-1);
front1 = rear1;
k = (arr[front1]/pow1(d-1))%10;
rear1+=1;
}
if(rear1==n)
{
radixsort(arr,front1,rear1,d-1);
front1 = rear1;
k = (arr[front1]/pow1(d-1))%10;
rear1+=1;
}
}
}
}