ICS 211 Homework H02: Sorting Arrays

Create an ArraySorter class that can sort Arrays.

Purpose

When we are dealing with data, we often want to put it in ‘order’. To do this we can sort it. In this assignment you will implement three simple sorts and analyze the sorting algorithms and your code.

In this assignment we have some specific requirements for your code. We have to satisfy the requirements of the problem in order to have correct solutions. This will give you early practice of writing code that meets some simple requirements.

Tasks

1. Create a package named edu.ics211.h02

This is where we will put all our classes for homework 02.

2. Create a generic class named ArraySorter that implements the SortableArray interface

public interface SortableArray<E> {
  void insertionSort(E[] data, Comparator<E> compare);
  void bubbleSort(E[] data, Comparator<E> compare);
  void selectionSort(E[] data, Comparator<E> compare);
  int getNumberOfSwaps();
  int getNumberOfComparisons();
  double getSortTime();
}

Each method should implement the named simple sort algorithm on the array data. Use the Comparator<E> to determine the order of the items in the array. Sort the arrays in ascending order. The ‘smallest’ element should be in position 0, ‘largest’ in data.length - 1.

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

3. Create some Comparators in package edu.ics211.h01

If you created them last week you are done. If not create them in package edu.ics211.h01.

Testing

We are going to use the FlightSorter.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. 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 doesn’t work, you should do this part to the best of your ability.

Grading Rubric

</tbody> </table> ## Turning in the Assignment The assignment is due on Friday at 11:55pm. You may turn it in early. 1. Does your code follow the [Java Coding Standard](/morea/010.introduction/reading-java-coding-standard.html) and pass CheckStyle? 2. Does your code pass the [JUnit Tests](FlightSorterTest.java)? 3. Export your project following the [instructions](../030.oop/experience-H01) at the bottom of the page. Name the file H02.zip 4. Sign into Laulima, then navigate to the ICS211 site. In the left hand side of the site, there is an Assignments tab/link. Click on it and view all of the posted assignments. Select the assignment that you want to turn in and attach your H02.zip file and your algorithm analysis accept the honor pledge to submit the assignment. Don't forget to include your analysis.
CriterionExcellent (100%)Satisfactory (75%)Borderline (50%)Unsatisfactory (25%)Poor (0)
Adherence to standards - 2 points
Does it conform to standards in every detail?
No errors. Minor details of the assignment are violated, or poor choices are made where the assignment is unclear. Significant details of the assignment or the underlying program intent are violated, but the program still fulfills essential functions. Significant details of the assignment or the underlying program intent are violated, but the program still fulfills some essential functions. Misses the point of the assignment.
Breakdown (modular design) - 1 point
Does it demonstrate good modular design?
No errors. 1-3 minor errors. > 3 minor errors OR 1 major error. 2 major errors > 2 major error.
Correctness of code - 2.5 points
Does it work? Does it pass JUnit?
Passes all tests. Works for typical input, may fail for minor special cases. Fails for typical input, for a minor reason. Fails for typical input, for a major reason. No.
Documentation, and style - 2 points
Is it clear and maintainable? Does it pass CheckStyle?
No errors. 1-3 minor errors. > 3 minor errors OR 1 major error. 2 major errors > 2 major error.
Efficiency of code - 1 point
Does it use the Java features well?
No errors. 1-3 minor errors. > 3 minor errors OR 1 major error. 2 major errors > 2 major error.
Algorithm Analysis - 2.5 points