-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix.cpp
More file actions
178 lines (177 loc) · 2.47 KB
/
radix.cpp
File metadata and controls
178 lines (177 loc) · 2.47 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
#include<windows.h>
void clrscr(void)
{
system("cls") ; //clear screen.
}
void gotoxy(short x, short y)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
typedef struct node
{
int no;
node *next;
}node;
typedef struct
{
node *front;
node *rear;
}que;
void init(que *qp)
{
qp->front=NULL;
qp->rear=NULL;
}
int isfull(que *qp)
{
return 0;
}
int isempty(que *qp)
{
if(qp->front==NULL)
return 1;
else
return 0;
}
void insert(que *qp,int n)
{
node *nd;
nd=(node*)malloc(sizeof(node));
if(nd==NULL)
{
printf("\n\nQUEUE IS FULL.");
exit(1);
}
nd->no=n;
nd->next=NULL;
if(isempty(qp)) //que is empty.
{
qp->front=nd;
}
else
{
qp->rear->next=nd;
}
qp->rear=nd;
}
int remove(que *qp)
{
node *t;
int x;
if(isempty(qp))
exit(2);
t=qp->front;
x=t->no;
qp->front=qp->front->next;
free(t);
if(qp->front==NULL)
qp->rear=NULL;
return x;
}
void printque(que *qp)
{
node *t=qp->front;
while(t!=NULL)
{
printf("%d\n",t->no);
t=t->next;
}
}
int highest(int *a,int n)
{
int i,h;
h=a[0];
for(i=1;i<n;i++)
{
if(a[i]>h)
h=a[i];
}
return h;
}
int countdigits(int n)
{
int c=0;
while(n!=0)
{
n=n/10;
c++;
}
return c;
}
int currentdigit(int n,int pos) //pos counted from end.
{
int x=0;
while(x<pos-1)
{
n=n/10;
x++;
}
n=n%10;
return n;
}
int* radixsort(int *a,int n)
{
int i,j,m,c;
que *q;
q=(que*)malloc(10*sizeof(que));
c=countdigits(highest(a,n));
for(i=0;i<10;i++) //creating array of ques.
{
init(&q[i]);
}
for(j=1;j<=c;j++) //no of iterations
{
for(i=0;i<n;i++)
{
insert(&q[currentdigit(a[i],j)],a[i]);
}
m=0;
for(i=0;i<n;i++)
{
while(isempty(&q[m]))
m++;
a[i]=remove(&q[m]);
}
}
free(q);
/*for(i=0;i<n;i++)
free(&q[i]);*/
return a;
}
int main()
{
int i,*a,n;
for(i=0;i<80;i++)
printf("-");
gotoxy(30,2);
printf(" RADIX SORT\n\n");
for(i=0;i<80;i++)
printf("-");
printf("\n\nHOW MANY ELEMENTS YOU WANT TO SORT?");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("\nENTER %dth ELEMENT:",i+1);
scanf("%d",&a[i]);
}
printf("\nTHE SORTED ELEMENTS ARE : \n\n");
a=radixsort(a,n);
for(i=0;i<n;i++)
printf("%d\n\n",a[i]);
printf("\n\nPRESS ANY KEY TO CONTINUE");
getch() ;
clrscr();
for(i=0;i<80;i++)
printf("-");
gotoxy(25,2);
printf(" THANK YOU!");
gotoxy(0,4);
for(i=0;i<80;i++)
printf("-");
}