Question

Written in Java Your job is to produce a program that sorts a list of numbers...

Written in Java

Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will display the sorted list back to the user.

A secondary task of this assignment is to provide runtime analysis of your program. As discussed in lecture, Big-O notation provides us a way to evaluate the performance of our code. You will provide in a separate text file the Big-O of the two sorting algorithms that we have implemented in this assignment.

This program will be written in Java.

Your program will be menu driven in which you will provide the following prompts to the user:

1. Load Integers (From File)

2. Exit Program

Once the user has loaded the list of integers the following options should be provided:

1. Insertion Sort

2. Bubble Sort

3. Exit Program

The text file containing integer data will be provided to you. The filename will be data.txt – this file can be found (and downloaded) on Canvas. The file will contain fifty integers (50) and they will be comma separated on a single line.

Example:

39,14,100,16,93,24,62,68,52,76,86,48,15,41,83,55,18,30,74,7,31,44,67,81

Your program must contain at-least four (4) classes.

o Driver

o Sort

o Insertion Sort

o Bubble Sort

o Quick Sort

You must use the concepts of Inheritance and Polymorphism in order to complete this assignment. o Your Sort class must be a pure virtual class.

o You should use proper OO approaches to implementing Inheritance.

• It is recommended you use stringstream to read-in and parse the data file.

• Your sample output must match that shown below – note that in order to “re-sort” the list you must also “re-load” the data file.

• You are required to use an Array to store the data read-in from the file.

• You are required to use the Heap to store your Objects in memory.

• You program should have no memory leaks – a memory leak will result in a deduction of points. o Make sure to run Valgrind against your submission to check for memory leaks and include this report with your submission.

• You are required to provide a brief (about one paragraph per algorithm) overview/analysis of the Big-O runtime of your program with respect to the two sorting algorithms we are implementing as part of this assignment – we will cover this in brief overview in lecture, you will need to provide some additional external research on your own to complete this assignment.

Below is the output of what your program should display when executed:

1. Load Data (From File)

2. Exit Program

Please enter your selection: 1

Unsorted Array: 39, 14, 100, 16, 93, 24, 62, 68, 52, 76, 86, 48, 15, 41, 83, 55, 18, 30, 74, 7, 31, 44, 67, 81, 70, 27, 53, 59, 61, 19, 56, 35, 88, 58, 72, 98, 38, 64, 94, 69, 50, 46, 78, 6, 57, 89, 26, 20, 79, 49

1. Insertion Sort

2. Bubble Sort

3. Exit Program

Please enter your selection: 1

Insertion Sort: 6, 7, 14, 15, 16, 18, 19, 20, 24, 26, 27, 30, 31, 35, 38, 39, 41, 44, 46, 48, 49, 50, 52, 53, 55, 56, 57, 58, 59, 61, 62, 64, 67, 68, 69, 70, 72, 74, 76, 78, 79, 81, 83, 86, 88, 89, 93, 94, 98, 100

1. Load Data (From File)

2. Exit Program

Please enter your selection: 1

Unsorted Array: 39, 14, 100, 16, 93, 24, 62, 68, 52, 76, 86, 48, 15, 41, 83, 55, 18, 30, 74, 7, 31, 44, 67, 81, 70, 27, 53, 59, 61, 19, 56, 35, 88, 58, 72, 98, 38, 64, 94, 69, 50, 46, 78, 6, 57, 89, 26, 20, 79, 49

1. Insertion Sort

2. Bubble Sort

3. Exit Program

Please enter your selection: 2

Bubble Sort: 6, 7, 14, 15, 16, 18, 19, 20, 24, 26, 27, 30, 31, 35, 38, 39, 41, 44, 46, 48, 49, 50, 52, 53, 55, 56, 57, 58, 59, 61, 62, 64, 67, 68, 69, 70, 72, 74, 76, 78, 79, 81, 83, 86, 88, 89, 93, 94, 98, 100

1. Load Data (From File)

2. Exit Program

Please enter your selection: 2

Goodbye!

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

package abc1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class FileSorting {
   private static final String FILENAME = "C:\\Users\\cr\\Desktop\\data22.txt";
   public static void insertionSort(int array[]) {
   int n = array.length;
   for (int j = 1; j < n; j++) {
   int key = array[j];
   int i = j-1;
   while ( (i > -1) && ( array [i] > key ) ) {
   array [i+1] = array [i];
   i--;
   }
   array[i+1] = key;
   }
   }
   private static int[] loadInteger() throws NumberFormatException, IOException{
       ArrayList<Integer> rows = new ArrayList<Integer>();
       int [] list=new int[50];
   BufferedReader reader = new BufferedReader(new FileReader(FILENAME));

   String s;
   while((s = reader.readLine())!=null)
   {
       String[]wordlist=s.split(",");
       for(int i=0;i<wordlist.length;i++){
           rows.add(Integer.parseInt(wordlist[i]));
           list[i]=Integer.parseInt(wordlist[i]);
           System.out.println(list[i]);
       }
      
   }
     

  
   reader.close();
   return list;

   }
   static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
  
}
}
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
  
}
public static void main(String[] args) throws IOException {
   int choice;
   Scanner scan=new Scanner(System.in);
   System.out.println("enter your choice ");
   choice=scan.nextInt();
   if(choice==1){
       int[] list=loadInteger();
       int choice2=scan.nextInt();
       if(choice2==1){
           insertionSort(list);
       }
       else if(choice2==2){
           bubbleSort(list);
       }
       else{
           System.out.println("bye");
          
       }
   }
  
   else{
       System.out.println("bye");
   }
   }
}

Add a comment
Know the answer?
Add Answer to:
Written in Java Your job is to produce a program that sorts a list of numbers...
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
  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • Pitcher 1 Pitcher 2 87 82 86 92 82 70 84 96 83 89 81 84 85 84 93 ...

    Pitcher 1 Pitcher 2 87 82 86 92 82 70 84 96 83 89 81 84 85 84 93 80 86 81 85 89 84 86 92 72 83 77 84 87 80 89 87 93 88 78 87 81 79 82 82 87 82 81 87 84 80 88 88 93 90 80 85 79 86 87 87 74 86 78 85 80 85 83 88 79 84 95 83 81 88 89 87 91 94 93 83 91...

  • PLEASE SHOW ME HOW TO DO THIS.... For the Excel Data Set please find and report...

    PLEASE SHOW ME HOW TO DO THIS.... For the Excel Data Set please find and report for Test 1 and Test 2 the Mean, SD, and the tolerance levels for both for which there would be any outliers (i.e., the value for which a score must be less than to be consider an outlier and the value for which a number must greater than to be considered an outlier. See picture Performance Data Group 1 1 1 1 Test 2...

  • 1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand....

    1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand. b. Explain why you chose this technique over others. Year 3 Year 1 Year 2 Actual Actual Actual Forecast Forecast Forecast Demand Demand Demand Week 1 52 57 63 55 66 77 Week 2 49 58 68 69 75 65 Week 3 47 50 58 65 80 74 Week 4 60 53 58 55 78 67 57 Week 5 49 57 64 76 77...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • 3.3 Table 3.10 shows the scores in the final examination F and the scores in two preliminary exam...

    3.3 Table 3.10 shows the scores in the final examination F and the scores in two preliminary examinations P1 and P2 for 22 students in a statistics course. The data can be found in the book's Web site. (a) Fit each of the following models to the data: Model 1 F Bo BiP Model 2 F- Model 3 : F-k) + AP,+AP, + ε Table 3.10 Examination Data: Scores in the Final (F), First Preliminary (Pi), and Second Preliminary (P2)...

  • 8. The following data are scores from a Physics final administered to 34 students. 81 76...

    8. The following data are scores from a Physics final administered to 34 students. 81 76 93 99 47 67 69 72 83 88 56 62 91 94 98 63 77 84 98 75 79 67 73 65 89 86 91 85 97 73 56 92 88 83 Use the Chart below to construct a Frequency Distribution with 5 classes (15 pts) Class Tally (This column is optional.) Frequency

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • 6. A bank with a branch located in Crown Point wants to develop an improved process...

    6. A bank with a branch located in Crown Point wants to develop an improved process for serving customers during the noon-to-lpm lunch period. Management decides to first study the waiting time of the current process. Data is collected from a random sample of 15 customer and their waiting time is recorded (in minutes) and is located in the Excel data file for this assignment under the tab CPBank. For comparative purposes, similar data is collected from a bank in...

  • Student stress at final exam time comes partly from the uncertainty of grades and the consequence...

    Student stress at final exam time comes partly from the uncertainty of grades and the consequences of those grades. Can knowledge of a midterm grade be used to predict a final exam grade? A random sample of 200 BCOM students from recent years was taken and their percentage grades on assignments, midterm exam, and final exam were recorded. Let’s examine the ability of midterm and assignment grades to predict final exam grades. The data are shown here: Assignment Midterm FinalExam...

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