DS JAVA_lab-radix Sort
DS JAVA_lab-radix Sort
DS JAVA_lab-radix Sort
Output:
Sorted array:
[0] [1] [2] [3] [4] [5] [6]
10 20 30 40 50 60 70
Radix Sort
This sorting algorithm is known as least-significant-digit-first radix sorting. Radix sorting is practical
for much larger universal sets. Radix sorting can be used when each element of the universal set can be
viewed as a sequence of digits (or letters or any other symbols).
An implementation of radix sort follows. The integer radix is assigned a value 10 and integer
maxDigits is also assigned the maximum number of digits for the maximum element of the array.
This algorithm does not depend on comparison of array elements. For each array element, two
operations are performed:
(1) To get the significant digit (m) of the number, the array element is divided by the divisor, and
division modulo radix(10).
(2) This array element is inserted in the mth queue.
In each pass, all array elements are added to the queues, and then the elements are deleted from all
the queues and stored back to array. The method requires additional space for queues. The ten queues
are implemented by linked lists using java.util.LinkedList class.
class RadixSortDemo
{
public static void main(String[] args)
{
int[] a = { 3305, 99, 52367, 125, 10, 12345, 7, 35, 7509, 3, 345 };
divisor = 1;
Output:
Sorted list:
3 7 10 35 99 125 345 3305 7509 12345 52367
class BSTNode
{ int data;
BSTNode left;
BSTNode right;
BSTNode( int d ) // constructor
{ data = d; }
}