Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP IN JAVA: In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. Your main program

HELP IN JAVA:

In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT.

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.

2. radixSort will NOT print the queue.

3. Use the java Queue class.

What I have so far:

public class H8 { public static void main(String[] args) {

String[] fileStrings = new String[25]; createArray(fileStrings); addZero(fileStrings); Queue myQueue = new LinkedList<>(); for (int i=0; i< fileStrings.length;i++){ myQueue.add(fileStrings[i]);

} radixSort(myQueue, fileStrings);

printArray(myQueue);

}

public static void printArray(Queue myQ) { for (int i = 0; i < myQ.size(); i++) { while(!myQ.isEmpty()){ System.out.print(myQ.remove()+ " "); } } System.out.println(); } public static void createArray (String[]nums){ File inFile=new File("radix.txt");

Scanner scnr=null; //Try/Catch try{ scnr=new Scanner(inFile); } catch(FileNotFoundException e){ } String temp = null; int count = 0; while (scnr.hasNext()){ temp = scnr.next(); nums[count] = temp; //System.out.println(nums[count]); count++; } }

public static int getLength(String[]nums){ int i, j; int max = 0; int count = 0; int size = nums.length; String s = null; for (i=0; i

if (s.length() > max){ max = s.length(); } } } return max; } public static void addZero(String[]nums){ int i; int size = nums.length; String a = null; String addedZeros = null; int d = 0; for (i = 0; i < size; i++){ a = nums[i]; d = Integer.parseInt(a); addedZeros = String.format ("%0"+getLength(nums)+"d", d); nums[i] = addedZeros; //System.out.println(nums[i]); } } public static Queue radixSort(Queue myQ, String[]nums) { Queue zero, one, two, three, four, five, six, seven, eight, nine; zero = new LinkedList(); one = new LinkedList(); two = new LinkedList(); three = new LinkedList(); four = new LinkedList(); five = new LinkedList(); six = new LinkedList(); seven = new LinkedList(); eight = new LinkedList(); nine = new LinkedList(); for (int len = getLength(nums) - 1; len >= 0; len--) { for (int i = 0; i < myQ.size(); i++) { int letter = nums[i].charAt(len); switch (letter) { case 0: zero.add(nums[i]); break; case 1: one.add(nums[i]); break; case 2: two.add(nums[i]); break; case 3: three.add(nums[i]); break; case 4: four.add(nums[i]); break; case 5: five.add(nums[i]); break; case 6: six.add(nums[i]); break; case 7: seven.add(nums[i]); break; case 8: eight.add(nums[i]); break; case 9: nine.add(nums[i]); break; } } int item = 0; while (!zero.isEmpty()) { nums[item] = (String) zero.remove(); myQ.add(nums[item]); item++; } while (!one.isEmpty()) { nums[item] = (String) one.remove(); myQ.add(nums[item]); item++; } while (!two.isEmpty()) { nums[item] = (String) two.remove(); myQ.add(nums[item]); item++; } while (!three.isEmpty()) { nums[item] = (String) three.remove(); myQ.add(nums[item]); item++; } while (!four.isEmpty()) { nums[item] = (String) four.remove(); myQ.add(nums[item]); item++; } while (!five.isEmpty()) { nums[item] = (String) five.remove(); myQ.add(nums[item]); item++; } while (!six.isEmpty()) { nums[item] = (String) six.remove(); myQ.add(nums[item]); item++; } while (!seven.isEmpty()) { nums[item] = (String) seven.remove(); myQ.add(nums[item]); item++; } while (!eight.isEmpty()) { nums[item] = (String) eight.remove(); myQ.add(nums[item]); item++; } while (!nine.isEmpty()) { nums[item] = (String) nine.remove(); myQ.add(nums[item]); item++; } } return myQ; } }

Need help figuring out why it is not sorting! Everytime I print it out its un sorted

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions