Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
60 views

Radix Code (Data Structures)

This document contains code for implementing a radix sort algorithm in Java. It defines a Radix class with a main method that calls the radixSort method, passing in two integer arrays to be sorted. The radixSort method uses queues to "bucket sort" the numbers based on the place value (ones, tens, etc.) from least to most significant digit, repeatedly inserting numbers into queues corresponding to their values at the current place and then extracting the numbers from the queues to reorder the array. It prints the arrays at various steps to demonstrate the sorting process.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Radix Code (Data Structures)

This document contains code for implementing a radix sort algorithm in Java. It defines a Radix class with a main method that calls the radixSort method, passing in two integer arrays to be sorted. The radixSort method uses queues to "bucket sort" the numbers based on the place value (ones, tens, etc.) from least to most significant digit, repeatedly inserting numbers into queues corresponding to their values at the current place and then extracting the numbers from the queues to reorder the array. It prints the arrays at various steps to demonstrate the sorting process.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package main;

import java.util.LinkedList;
import java.util.Queue;

/**
* Implementation of radix sort.
*
* @author Anton Gustafsson
*
*/
public class Radix {

public static void main(String[] args) {


new Radix();
}

/**
* Array with numbers to be sorted.
*/
public Radix() {
int[] unsorted1 = { 9, 179, 139, 38, 10, 5, 36 };
int[] unsorted2 = { 667342, 1745359, 556139, 343538, 5510, 45, 5536 };
radixSort(unsorted1);
}

/**
* Performs radix sort, a kind of bucket sort. From right to left, take the
* least significant number and put them in their corresponding bucket. Then
* take them out, and that value is now sorted. Then do it again for all
* valules, ex first "ones", then "tens" and so on.
*
* @param unsorted
*/
public void radixSort(int[] unsorted) {
// Create a box with 10 buckets.
Queue[] box = new Queue[10];
for (int i = 0; i < box.length; i++) {
box[i] = new LinkedList<Integer>();
}

System.out.print("Unsorted list: ");


printArray(unsorted);
// Find the longest number in the unsorted array.
int length = 0;
for (int i = 0; i < unsorted.length; i++) {
int n = (int) (Math.log10(unsorted[i]) + 1);
if (n > length) {
length = n;
}
}

int m = 10; // The number to use modulo on


int n = 1; // The number for rounding division.
// Repeats as long as the highest number in the array.
for (int i = 0; i < length; i++) {

// Perform calculation and put element in correct bucket.


for (int y = 0; y < unsorted.length; y++) {
int nbr = (unsorted[y] % m) / n;
box[nbr].add(unsorted[y]);
}

// Now remove the numbers in order and put them in a new array.
int x = 0;
for (int j = 0; j < box.length; j++) {
while (!box[j].isEmpty()) {
unsorted[x] = (int) box[j].remove();
x++;
}
}
System.out.print("Partly Sorted " + n + "'s: (");
printArray(unsorted);
m *= 10;
n *= 10;
}
System.out.print("Sorting complete: ");
printArray(unsorted);
}

/**
* Prints the given arrays elements.
*
* @param arr
*/
private void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ", ");
}

System.out.println(")");
}
}

You might also like