Question

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 directory as the input file.

5. The program shall write 1 integer per line in the output file. 6. The program shall use a recursive algorithm to sort the values.

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

PLEASE GIVE THUMBS UP, THANKS

SAMPLE OUTPUT :

code:


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
*
* @author VISHAL
*/
public class RecursiveSort {
// 1. A recursive function to implement bubble sort sorting algorith
static void bubbleSort(int arr[], int n)
{
// Base case
if (n == 1)
return;
  
// One pass of bubble sort. After
// this pass, the largest element
// is moved (or bubbled) to end.
for (int i=0; i<n-1; i++)
if (arr[i] < arr[i+1])
{
// swap arr[i], arr[i+1]
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
  
// Largest element is fixed,
// recur for remaining array
bubbleSort(arr, n-1);
}
public static void main(String[]args) throws IOException
{
String filename;
String outfilename;
Scanner sc;
FileWriter wr;
ArrayList<Integer> A=new ArrayList<Integer>();
//2. Reading filename Using GUI
filename=JOptionPane.showInputDialog("Enter Input File Name : ");
outfilename=JOptionPane.showInputDialog("Enter OUTPUT File Name : ");
try {
sc=new Scanner(new File(filename));
wr=new FileWriter(outfilename);
} catch (FileNotFoundException ex) {
System.out.println("FILE NOT FOUND, PLEASE CHECK IT");
return;
}
//reading numbers from file
while(sc.hasNextLine())
{
A.add(Integer.parseInt(sc.nextLine()));
}
int []Array=new int[A.size()];
int n=A.size();
for(int i=0; i<A.size(); i++)
{
Array[i]=A.get(i);
}
//doing sorting
bubbleSort(Array,n);
//writing to file
for(int i=0; i<n; i++)
{
wr.write(Array[i]+"\n");
}
wr.close();
sc.close();
System.out.println("Number are written to OUTPUT FILE : "+ outfilename);
}
}

Add a comment
Know the answer?
Add Answer to:
Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should...
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
  • 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.

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

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