Question

In: Computer Science

IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...

IN JAVA!!!

In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999.

Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main.

The main program will then call another method to print the contents of this (sorted queue) .

Notes:

1. radixSort method will need an array of queues. (you had a similar assignment, H4, where you needed an array of linked lists. You may need to look this up to refresh how to create an array of data structures. H4 was an array of linked lists, this is an array of queues - do NOT make it an array of linked lists)

2.  radixSort will NOT print the queue.

3. Use the java Queue class (the Interface).

radix.txt

2345 67 89 1145 36 39 40 11 95 4707 299 278 256 6189 170 8167 1459 1234 1226 998 889 4434 3912 4570 66

Solutions

Expert Solution

file-name:   RadixSort.java
-------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class RadixSort {
    public static void main(String[] args) {
        Queue<Integer> radixQueue = new LinkedList<>();

        BufferedReader br ;
        try {
            br = new BufferedReader(new FileReader("/home/rahul/Pictures/radix.txt"));
            String line = br.readLine();
            while(line != null) {
                String[] str = line.split(" ");
                int i = 0;
                while (i < str.length) {
                    radixQueue.add(Integer.parseInt(str[i]));
                    i++;
                }
                line = br.readLine();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Before sorting");
        printQueue(radixQueue);
        // calling the radixSort method to sort the elements in the Queue
        Queue<Integer> sorted = radixSort(radixQueue);
        System.out.println("\n--------------------------------------");
        System.out.println("After sorting");
        // printing
        printQueue(sorted);

    } // END of main() method

    public static Queue<Integer> radixSort(Queue<Integer> Q) {
       int max =  getMax(Q);

       for(int exp =1; max/exp > 0; exp = exp*10) {
           Q = countSort(Q,exp);
       }
        return Q;
    }

    public static int getMax(Queue<Integer> Q) {
        // System.out.println("MAX value: "+Collections.max(Q));
        return  Collections.max(Q);
    }

    public static Queue<Integer> countSort(Queue<Integer> Q, int exp) {
        ArrayList<Integer> al = new ArrayList<>(Q);
        int output[] = new int[al.size()];
        int count[] = new int[10];
        Arrays.fill(count, 0);

        for(int i: al) {
            count[(i/exp)%10]++;
        }
//        for(int i=0;i<10;i++) {
//            System.out.println(count[i]);
//        }
        for(int i=1; i<10;i++) {
            count[i] = count[i] + count[i-1];
        }

        for(int i = al.size()-1; i>=0;i--) {
            output[count[(al.get(i)/exp)%10] -1] = al.get(i);
            count[(al.get(i)/exp)%10]--;
        }
        Queue<Integer> new_q = new LinkedList<>();
        for(int i : output) {
            new_q.add(i);
        }
        return new_q;
    }

    public static void printQueue(Queue<Integer> Q) {
        Iterator iterator = Q.iterator();

        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
} // END of class

---------------------------------------------------------------------------

OUTPUT:

The one in white background is output on console and the one in background is input file.

THANK YOU!!


Related Solutions

Submit a java file. Write a program where you input 4 numbers and the output generates...
Submit a java file. Write a program where you input 4 numbers and the output generates the average of the 4 numbers. The output may not be negative and <= 100. If any value is negative or >100, it should be replaced by the value of 30. For all such values, you will replace them with a value of 10. The program should produce the following output: Today's average amount is: $xx.xx.
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
USING THE IF STATEMENT & Scanner for input and println for output - Write a java...
USING THE IF STATEMENT & Scanner for input and println for output - Write a java program where the user enters a temperature as a whole number from input, and outputs a “most likely” season [either SUMMER, SPRING, FALL or WINTER] depending on the temperature entered. SUMMER would be 90 or higher SPRING   would be 70 to less than 90 FALL       would be 50 to less than 70 WINTER would be less than 50 Consider it an error if the...
Java Input, Output and for-loop function. Practice01) Write-an average SCORE application called TestScores01 This project reads...
Java Input, Output and for-loop function. Practice01) Write-an average SCORE application called TestScores01 This project reads in a list of integers as SCOREs, one per line, until a sentinel value of -1. After user type in -1, the application should print out how many SCOREs are typed in, what is the max SCORE, the 2nd max SCORE, and the min SCORE, the average SCORE after removing the max and min SCORE. When SCORE >= 90, the school will give this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT