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

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 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...
java program: Input and output the following details. Your program can only receive the correct input....
java program: Input and output the following details. Your program can only receive the correct input. (For example: a notification will be given if you not enter Char data type for Name) Name: Ali bin Ahmad Occupation: Technician Age: 30 Hometown: Negeri Sembilan Years of Service: 12 Gender: Male
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT