Question

Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....

Objective:

     Implement a sorting algorithm.


Description:

     Implement a radix sort in a Java class named RadixSort.java.

Your program should receive its input from a file named "input.txt",
which contains one integer per line.

It should produce a sorted output file named "output.txt".

Include a main method which demonstrates that your algorithm works.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.io.*;
import java.util.*;

public class RadixSort {

   // function to get maximum value
   static int maxValue(int arr[], int n)
   {
       int mx = arr[0];
       for (int i = 1; i < n; i++)
           if (arr[i] > mx)
               mx = arr[i];
       return mx;
   }
   static void countSort(int arr[], int n, int exp)
   {
       int output[] = new int[n]; // output array
       int i;
       int count[] = new int[10];
       Arrays.fill(count,0);

       // Store count of occurrences in count[]
       for (i = 0; i < n; i++)
           count[ (arr[i]/exp)%10 ]++;

       // Change count[i] so that count[i] now contains
       // actual position of this digit in output[]
       for (i = 1; i < 10; i++)
           count[i] += count[i - 1];

       // Build the output array
       for (i = n - 1; i >= 0; i--)
       {
           output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
           count[ (arr[i]/exp)%10 ]--;
       }
       //Copy output array to arra[]
       for (i = 0; i < n; i++)
           arr[i] = output[i];
   }

   // The main function to that sorts arr[] of size n using
   // Radix Sort
   static void radixsort(int arr[], int n)
   {
       // Find the maximum number to know number of digits
       int m = maxValue(arr, n);

       // Do counting sort for every digit. Note that instead
       // of passing digit number, exp is passed. exp is 10^i
       // where i is current digit number
       for (int exp = 1; m/exp > 0; exp *= 10)
           countSort(arr, n, exp);
   }

   // A utility function to print an array
   static void print(int arr[], int n)
   {
       for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
   }
  
   public static void writeSortedarray(String fileName,int arr[])
   {
   try {
  
   Writer wr = new FileWriter(fileName);
for (int i = 0; i < arr.length; i++)
{
wr.write(arr[i] + "");
wr.write("\r\n");
}
wr.close();
}
catch (IOException e)
{
System.out.println("Error - " + e.toString());
}
   }
  
public static int[] readIntegers(String fileName)
{
try{
File inputFile=new File(fileName);
Scanner s=new Scanner(inputFile);
int ctr=0;
while(s.hasNextInt())
{
ctr++;
s.nextInt();
}
int[] arr=new int[ctr];
Scanner s1=new Scanner(inputFile);
for (int i=0;i<arr.length;i++)
arr[i]=s1.nextInt();
return arr;
}
catch(Exception ex)
{
return null;
}
}

   /*Driver function to check for above function*/
   public static void main (String[] args)
   {
       int[] arr = readIntegers("input.txt");
   int n = arr.length;
   radixsort(arr, n);
   writeSortedarray("output.txt",arr);
   }
}

Add a comment
Know the answer?
Add Answer to:
Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

  • PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input...

    PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input by radix, bucket sort (with no insertion sort step) once for each radix starting from the least significant. Make sure that your overall implementation is O(n) NPUT The input to your program will an unspecified number of entries. Each entry is a non-negative integer containing nine (zero padded) digits ( this means that the integer may have either leading or trailing zeros), one per...

  • Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should...

    Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...

  • Need a quick solution to this in Java! OPTION 2: RADIX SORT TESTING Write a version...

    Need a quick solution to this in Java! OPTION 2: RADIX SORT TESTING Write a version of Radix Sort to sort an ArrayList or array of ints / Integers that you will place in a file. The integers should all be four digits long. Run the algorithm on at least 3 different files, of different sizes (i.e., each file should have a different number of integers) and have it print the results. The minimum file size should be 20 integers....

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • please do by java P14.6 Implement the radix sort algorithm described in Exercise R14.22 (below) to...

    please do by java P14.6 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arrays of numbers between 0 and 999. P14.7 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arrays of numbers between 0 and 999. However, use a single auxiliary array, not ten. .P14.8 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arbitrary int values (positive or negative

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...

    FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named TextBookSort.java. Include these steps: Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price. This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java. Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for...

  • Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java...

    Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java and a minimum of two threads utilised.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT