Question: Please help me.. 2. Create a generic class named ArraySorter that implements the SortsArray interface public interface SortsArray { void insertionSort(Comparator compare, E[] data); void
Please help me..
2. Create a generic class named ArraySorter that implements the SortsArray interface
public interface SortsArray{ void insertionSort(Comparator compare, E[] data); void bubbleSort(Comparator compare, E[] data); void selectionSort(Comparator compare, E[] data); int getNumberOfSwaps(); int getNumberOfComparisons(); double getSortTime(); }
Each method should implement the named simple sort algorithm on the array data. Use the Comparator
The methods should keep track of the number of comparisons, number of swaps and the amount of time it took to sort the array. To record the time taken, your code should call System.nanoTime at the beginning of each of the methods and save the value as the start time. It should then call System.nanoTime again at the end, and print the difference between the two times. The difference may be zero if your system clock does not update the time quickly enough, but otherwise should accurately report how many nanoseconds (billionths of a second) it took to execute your code. When the sort methods complete they should report the number of comparison, swaps, and the execution time in nanoseconds.
Requirements
-
Ensure that your implementation has the following characteristics:
-
The BigO for insertion and bubble sort on sorted data must be BigO(n);
-
The number of swaps for all three sorts on sorted data must be 0.
-
3. Create some Comparators in package edu.ics211.h02
-
Create a FatComparator that implements Comparator
deciding order based upon the Cheeses fat percentage values. -
Create an TypeComparator that implements Comparator
deciding order based upon the type of the Cheeses.
If you created them last week you are done. If not create them in package edu.ics211.h02.
Testing
We are going to use the CheeseSorterTest.java JUnit tests to evaluate your homework for correctness.
You may add any more tests if you want to.
Algorithm Analysis
Write up a runtime analysis of each of the sorting methods, giving the big-O of each, and explaining how you got your results. Your analysis should refer to your code by line number. Send your analysis to the TA together with your code. This part counts for 25% of the grade on this assignment. Even if your code doesnt work, you should do this part to the best of your ability.
Part 2
Completing this part will help you with Homework 11: Huffman Trees.
Create a ReadFile class that reads in a file
We are going to continue exploring handling binary data in Java. You will write a class that reads in a file with a specific format.
The file has the following format:
- An integer, the number of bytes in the String.
- A byte, the encoding of the String.
- 1 means the String is encoded using StandardCharsets.US_ASCII
- 2 means the String is encoded using StandardCharsets.UTF_16LE
- 3 means the String is encoded using StandardCharsets.UTF_8
- 4 means the String is encoded using StandardCharsets.UTF_16
- The bytes that make up the String.
Tasks
1. Create a class named ReadFile that implements the IReadFile interface
The IReadFile interface has one method in it.
public interface IReadFile { /** * Reads in the given file and returns the contents of the file as a string. * The file has the following format:
* - *
- An integer, the number of bytes the String has. *
- A byte, the encoding of the String. *
- StandardCharsets.US_ASCII *
- StandardCharsets.UTF_16LE *
- StandardCharsets.UTF_8 *
- StandardCharsets.UTF_16 *
- The String as bytes. *
- *
2. Implement the readFile method
What are the steps you need to read in the contents of the file?
Java has a nice class DataInputStream that allows you to read in different types. I recommend you use this class. Also the String class has a nice constructor that takes an array of bytes and a Charset.
import java.util.Comparator;
/**
* @author .
*
* @param
*/
public class ArrarySorter
private int numSwaps;
private int numComps;
private double sortTime;
/**
*
*/
public ArrarySorter() {
}
@Override
public void insertionSort(Comparator
long startTime = System.nanoTime();
int n = data.length;
for (int fill = 0; fill < n - 1; fill++) {
}
E nextVal = data[numComps];
while (numComps < 0 && compare.compare(nextVal.data[numComps - 1]) < 0) {
data[numComps] = data[numComps - 1];
numComps--;
}
double endTime = System.nanoTime() - getSortTime();
}
@Override
public void bubbleSort(Comparator
long startTime = System.nanoTime();
int c = data.length;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length - i; j++) {
E nextVal = data[numComps];
if (data[j],data[j + 1] > 0) {
swap(data[j], data[j + 1]);
}
}
}
}
@Override
public void selectionSort(Comparator
long startTime = System.nanoTime();
int n = data.length;
for (int i = 0; i < data.length; i++) {
int posMin = i;
for (int j = i; j < data.length; j++) {
if (data[i].compare.compare(data[posMin]) < 0) {
posMin = i;
}
E temp = data[(int) sortTime];
data[i] = data[posMin];
data[posMin] = temp;
}
sortTime++;
double endTime = System.nanoTime() - getSortTime();
}
}
@Override
public int getNumberOfSwaps() {
return numSwaps;
}
@Override
public int getNumberOfComparisons() {
return numComps;
}
@Override
public double getSortTime() {
return sortTime;
}
}
package edu.ics211.h03;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Represents a IReadFile.
*
* @author .
*
*/
public interface IReadFile {
/**
* Reads in the given file and returns the contents of the file as a string.
*
The file has the following format:
*
- An integer, the number of bytes the String has.
- A byte, the encoding of the String.
- StandardCharsets.US_ASCII
- StandardCharsets.UTF_16LE
- StandardCharsets.UTF_8
- StandardCharsets.UTF_16
- The String as bytes.
*
*
*
*
*
*
*
*
*
*
* @param fileName The name of the file.
* @return The String that was encoded in the file.
* @throws FileNotFoundException If there is a problem with the file name.
* @throws IOException If there is a problem reading the file.
*/
String readFile(String fileName) throws IOException;
}
package edu.ics211.h03;
import java.io.IOException;
/**
* @author .
*
*/
public class ReadFile implements IReadFile {
/**
*
*/
public ReadFile() {
// TODO Auto-generated constructor stub
}
@Override
public String readFile(String fileName) throws IOException {
DataInputStream in new = DataInputStream (new FileInputStream(fileName));
return null;
}
}
import java.util.Comparator;
/**
* Interface for a SortableArray.
*
* @author .
* @param
*/
public interface SortsArray
/**
* Sorts the array data using the insertion sort.
* @param compare the Comparator that determines order.
* @param data the data.
*/
void insertionSort(Comparator
/**
* Sorts the array data using the bubble sort.
* @param compare the Comparator that determines order.
* @param data the data.
*/
void bubbleSort(Comparator
/**
* Uses the selection sort algorithm to sort the data array.
* @param compare a Comparator.
* @param data the data to sort.
*/
void selectionSort(Comparator
/**
* Returns the number of swaps for the last sort.
*
* @return the number of swaps for the last sort.
*/
int getNumberOfSwaps();
/**
* Returns the number of comparisons for the last sort.
*
* @return the number of comparisons for the last sort.
*/
int getNumberOfComparisons();
/**
* Returns the time it took to sort.
*
* @return the time it took to sort.
*/
double getSortTime();
}
Please implement ReadFile class and ArraySorter class. Also, please make comparators (two).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
