Question

The assignment was to write a radix sort program that read from a list of numbers but I cannot figure it out past getting the number of digits for the highest number

So far I read the numbers from a file (numbers.txt) and then print them into an ArrayList called numbers. I then go through numbers and find out the largest number and what its 10's place is.

Then from their I am lost and not sure how to do the rest

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class RadixSort {
    final static int[] sizeArray = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, Integer.MAX_VALUE};
    public static void main(String [] args){
        ArrayList <Integer> numbers= new ArrayList <Integer>();
        try {
            Scanner sc=new Scanner(new File("numbers.txt"));
            while(sc.hasNext()){
                numbers.add(sc.nextInt());
            }//end while
            PrintArray(numbers);
            System.out.println();
        } //end try
        catch (FileNotFoundException e) {
            System.out.println("The file cannot be found");
        }//end catch
        int digits=FindDigits(numbers);
        Radix(numbers, digits);
    }//end main

    public static void PrintArray(ArrayList<Integer> a){
        for(int i=0; i<a.size(); i++){
            System.out.print(a.get(i) + " ");
        }//end for
    }//end printArray

    public static int FindDigits(ArrayList<Integer> numbers){
        int biggest=numbers.get(0);
        for(int i=0; i<numbers.size();i++){
            if(numbers.get(i)>biggest){
                biggest=numbers.get(i);
            }//end if
        }//end for
        if(biggest==Integer.MAX_VALUE){
            return 10;
        }//end if
        else{
        for(int i=0;i<sizeArray.length; i++){
            if(biggest/sizeArray[i]==0){
                return i+1;
            }//end if
        }//end for
        }//end else
        return 1;
    }//end find digits
    public static void Radix(ArrayList<Integer> numbers, int digits){
        ArrayList<Integer> sorted= new ArrayList<Integer>();
        int divisor=1;
        int digit;
        for(int i=0; i<digits;i++){
            divisor*=10;
        }//end for
        for(int i=0; i<numbers.size();i++){
            digit=numbers.get(i)%divisor;
            if(digit/10>0){
                digit=(digit/(digits-1));
                sorted.set(digit, numbers.get(i));
            }//end if
            else{
                sorted.set(digit, numbers.get(i));
            }//end else
        }//end for
        PrintArray(sorted);
    }//end radix
}//end class
Was it helpful?

Solution

This is not what stack overflow questions are meant to be like. Try to get further with your problem and ask for specifics here. Please read the stack overflow question guide

https://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist

Your question does not demonstrate enough understanding or research there is heaps of info on this topic available.

Ie, the first result in google is: http://web.engr.oregonstate.edu/~budd/Books/jds/info/src/jds/sort/RadixSort.java

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top