Skip to content

Commit 4593448

Browse files
Radix Sort in Java
1 parent 6e350e3 commit 4593448

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

sorting/radix-sort/Node.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
7+
public class Node
8+
{
9+
public int info;
10+
public Node link;
11+
12+
public Node(int i)
13+
{
14+
info=i;
15+
link=null;
16+
}
17+
}
18+
19+

sorting/radix-sort/RadixSort.java

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class RadixSort
9+
{
10+
public static Node sort(Node start)
11+
{
12+
Node[] rear = new Node[10];
13+
Node[] front = new Node[10];
14+
15+
int leastSigPos=1;
16+
int mostSigPos=digitsInLargest(start);
17+
18+
int i,dig;
19+
Node p;
20+
for(int k=leastSigPos; k<=mostSigPos; k++)
21+
{
22+
/*Making all the queues empty at the beginning of each pass*/
23+
for(i=0; i<=9 ; i++)
24+
{
25+
rear[i] = null;
26+
front[i] = null;
27+
}
28+
29+
for(p=start; p!=null; p=p.link)
30+
{
31+
/*Find kth digit from right in the number*/
32+
dig = digit(p.info,k);
33+
34+
/*Insert the node in Queue(dig) */
35+
if(front[dig] == null)
36+
front[dig]=p;
37+
else
38+
rear[dig].link=p;
39+
rear[dig]=p;
40+
}
41+
42+
/*Join all queues to form new linked list*/
43+
i=0;
44+
while(front[i] == null) /*Finding first non empty queue*/
45+
i++;
46+
start = front[i];
47+
while(i<=8)
48+
{
49+
if(rear[i+1]!=null) /*if (i+1)th queue is not empty*/
50+
rear[i].link=front[i+1]; /*join end of ith queue to start of (i+1)th queue*/
51+
else
52+
rear[i+1]=rear[i]; /*continue with rear[i]*/
53+
i++;
54+
}
55+
rear[9].link=null;
56+
}
57+
return start;
58+
}/*End of sort*/
59+
60+
/*Returns number of digits in the largest element of the list */
61+
public static int digitsInLargest(Node start)
62+
{
63+
/*Find largest element*/
64+
int large=0;
65+
Node p=start;
66+
while(p!=null)
67+
{
68+
if(p.info > large)
69+
large = p.info;
70+
p=p.link;
71+
}
72+
73+
/*Find number of digits in largest element*/
74+
int ndigits=0;
75+
while(large!=0)
76+
{
77+
ndigits++;
78+
large/=10;
79+
}
80+
return ndigits;
81+
}
82+
83+
/*Returns kth digit from right in n*/
84+
public static int digit(int n, int k)
85+
{
86+
int d=0,i;
87+
for(i=1; i<=k; i++)
88+
{
89+
d=n%10;
90+
n/=10;
91+
}
92+
return d;
93+
}
94+
95+
public static void main(String[] args)
96+
{
97+
Node temp,p;
98+
int i,n,data;
99+
100+
Scanner scan = new Scanner(System.in);
101+
102+
System.out.println("Enter number of elements in the list : ");
103+
n = scan.nextInt();
104+
105+
Node start=null;
106+
for(i=1; i<=n; i++) /*Inserting elements in linked list*/
107+
{
108+
System.out.print("Enter element " + i + " ");
109+
data = scan.nextInt();
110+
111+
temp = new Node(data);
112+
if(start==null)
113+
start=temp;
114+
else
115+
{
116+
p=start;
117+
while(p.link!=null)
118+
p=p.link;
119+
p.link=temp;
120+
}
121+
}
122+
123+
start = sort(start);
124+
125+
System.out.println("Sorted list is :\n");
126+
p=start;
127+
while(p!=null)
128+
{
129+
System.out.print(p.info + " ");
130+
p=p.link;
131+
}
132+
System.out.println();
133+
scan.close();
134+
}
135+
}

0 commit comments

Comments
 (0)