From Lab 14a
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
String line = null;
while ((line = reader.readLine()) != null) {
int num = Integer.parseInt(line);
numbers.add(num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sorts.performBubbleSort(numbers);
for (int num : numbers) {
System.out.println(num);
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter("sorted_data.txt"))) {
for (int num : numbers) {
writer.write(num + "
");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sorts.java
import java.util.ArrayList;
public class Sorts {
public static void performBubbleSort(ArrayList<Integer> toBeSorted) {
int temp = 0;
boolean swapMade = true;
for (int i = 0; i < toBeSorted.size() - 1; i++) {
if (!swapMade)
break;
swapMade = false;
for (int j = 0; j < toBeSorted.size() - 1 - i; j++)
if (toBeSorted.get(j) > toBeSorted.get(j + 1)) {
temp = toBeSorted.get(j);
toBeSorted.set(j, toBeSorted.get(j + 1));
toBeSorted.set(j + 1, temp);
swapMade = true;
}
}
}
public static void performInsertionSort(ArrayList<Integer> toBeSorted) {
}
public static ArrayList<Integer> performMergeSort(ArrayList<Integer> toBeSorted) {
return new ArrayList<Integer>();
}
private static ArrayList<Integer> merge(ArrayList<Integer> listOne, ArrayList<Integer> listTwo) {
return new ArrayList<Integer>();
}
}
Graded Assignment 5 - Insertion and Merge Sorts
Supporting files
Both the files from Lab 14a (completed)
Assignment objectives
Code, test, and use an insertion sort method
Code, test, and use a merge sort method
Assignment submission
All graded assignments must be submitted by 11:59 p.m. on their corresponding due date. Once completed, upload the compressed file to Blackboard.
Assignment directions
Note: this assignment will build onto lab 14a, so that lab should be completed prior to starting this one. The supporting files are provided here for convenience but they are the same files as those posted in lab 14a.
Part 1 - insertion sort
1. In the driver (MyClass), comment out the code associated with the bubble sort (but be sure to leave the code in the file)
2. Write an insertion sort method (i.e. complete the stub provided in the Sorts class)
3. Add code to your driver (MyClass) so that it uses your insertion sort to display the sorted numbers to the screen, and test it to ensure it is still producing a correctly sorted list
4. Add code to your driver (MyClass) so that in addition to displaying the sorted numbers to the screen, it also writes the sorted numbers to a file called "insertion.txt"
Part 2 - merge sort
5. In the driver (MyClass), comment out the code associated with the insertion sort (but be sure to leave the code in the file)
6. Write a merge sort method (i.e. complete the stub provided in the Sorts class)
7. Add code to your driver (MyClass) so that it uses your merge sort to display the sorted numbers to the screen, and test it to ensure it is still producing a correctly sorted list
8. Add code to your driver (MyClass) so that in addition to displaying the sorted numbers to the screen, it also writes the sorted numbers to a file called "merge.txt"