Radix Sort in Java
Radix Sort in Java
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc)
is Ω(nLogn), i.e., they cannot do better than nLogn.
Counting sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in range
from 1 to k.
Example:
Original, unsorted list: 170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802
occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2
in the previous list.]
class Radix {
Output:
2 24 45 66 75 90 170 802
Next we arrange every number considering 1s place value, 10s place value, 100s place value
and so on till the length of the maximum digit.
1. Find the length of the number that has maximum number of digits.
2. Initialize i=0, Repeat the below procedure till the length equals i.
3. Fill the bucket with all the digits in ith position.
4. Sort out the digits according to the order.
5. If length=i, i=i*10, goto to step 3. Else go to step 5
6. Print the sorted array.
Complexity
The complexity of Radix Sort is far better than that of bubble sort and some other sorting techniques.
It is as shown below depends on d and b.
O (d*(n+b))
d is digits in input integers.
b is the base for representing numbers.
Explanation
Let us start the implementation of the program. First we define a class named RadixSort and
obviously it has only one method named sort to sort the numbers given. This function is the central
part of discussion and as always, we take the inputs for the user using the main function and pass it on
to the sorting function. The below code snippet shows it.
sort(arr);
You can observe that the function that we are going to implement doesn’t return anything as its return
type is void. It takes in an unsorted array of numbers and sorts it. Now we will have a look at the
algorithm to implement the sort function. The algorithm says we need to first find the number of
digits in the longest number.
Using the above for loop we can find the maximum of all the numbers. Instead of finding its length
we will iterate a while loop until the maximum number falls below zero by dividing the number by 10
for every iteration. Then sort the array using a temporary bucket and array b[ ] and place back the
final contents.
class RadixSort
{
static void sort( int[] a)
{
int i, m = a[0], exp = 1, n = a.length;
int[] b = new int[10];
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
sort(arr);
System.out.println();
}
}
Output
class BubbleSort
{
public static void main(String...s)
{
int a[]=new int[20],n,i,j,temp;
for(i=0;i<n;++i)
a[i]=sc.nextInt();
for(i=1;i<n;++i)
for(j=0;j<n-i;++j)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
for(i=0;i<n;++i)
System.out.print(a[i]+" ");
}
}
class BinarySearch
{
public static void main(String ar[])
{ int i,mid,first,last,x,n,flag=0;
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(a[mid]>x)
last=mid-1;
else
if(a[mid]<x)
first=mid+1;
else
{
flag=1;
System.out.println("nelement found");
break;
}
}
if(flag==0)
System.out.println("nelement not found");
}
}