CS367 Programming Assignment 5
Lecture 4, Fall 2016
FAQ

Some frequently asked questions (and their answers)

  1. What counts as a data move?
  2. The numbers I'm getting are different from the numbers in the sample. Is that important?
  3. The times I am getting are really different than the ones in the sample although the number of compares and data moves is about the same. Is something wrong?
  4. How far should I take my quick sort and merge sort (i.e., down to subarrays of size what)?
  5. My numbers for quick sort are a lot bigger than yours. Is that OK?
  6. Am I allowed to modify the SortObject class?
  7. How do create an array of generics, i.e., E[]?

Q1: What counts as a data move?

Any assignment to an item from a class that implements the Comparable interface (such as SortObject) counts as a data move. For example, given an array A containing items of type E where E implements Comparable, the following code does two data moves.:

E temp = A[0];
A[0] = A[3];

Q2: The numbers I'm getting are different from the numbers in the sample. Is that important?

We are not expecting everyone to get exactly the same numbers as in the sample output. We may or may not have done a small optimization in some places that you may or may not have done. As a general rule, if the numbers you are getting are within a factor of two of ours then you probably don't need to worry about it. So, if the number we got using an array of size 5000 was 60,000 and you got 65,000 or 55,000 then that's OK. But if we got 60,000 and you got 600,000 then that's a problem.

For selection sort, if you use the code from the on-line reading, you'll find that your code does a lot more data moves than our program (e.g., about three times as many on an input array of size 5000). That's ok - when we implemented selection sort, we used a variant of this algorithm which does a lot fewer data moves.

Q3: The times I am getting are really different than the ones in the sample although the number of compares and data moves is about the same. Is something wrong?

Not necessarily. Your times may vary depending on what machine you are running the program on, how many other people are also on that machine, what other programs are currently running, etc. If you find that you keep getting a time of 0 for one or two sorts, you may need to increase the size of the array (to say 7000 or 10000) to be able to measure the running time of the sort(s).

Q4: How far should I take my quick sort and merge sort (i.e., down to subarrays of size what)?

You should do all sorts as far as possible. For merge sort, that means you keep doing merge sort until you get to a subarray of size 1. For quick sort, you keep doing quick sort until you get to size 1 or 2. At that point the on-line reading says to do insertion sort on the subarray. That's really not necessary for us (since it requires you to implement a second version of insertion sort); it only takes a couple lines of code to sort a subarray of size 1 or 2.

Q5: My numbers for quick sort are a lot bigger than yours. Is that OK?

No. If you are using the code from the on-line reading, then when quick sort reaches a subarray of size 1 or 2 it switches to insertion sort. But this is not the same insertion sort that you implement elsewhere for this assignment. The call to insertion sort in the on-line notes passes the array and low and high indices. What this version of insertion sort is supposed to do is sort the array only from index low to index high. The insertion sort you implement for this assignment takes only the array as a parameter and it sorts the entire array. So, unless you write a second version of insertion sort, each quick sort on a subarray of size 1 or 2 will have insertion sort sort the whole array (not just the subarray). You can avoid writing a second version of insertion sort by just putting the code that sorts a subarray of size 1 or 2 directly into the appropriate place in the quick sort code.

Q6: Am I allowed to modify the SortObject class?

No. You must use the SortObject class as it was given.

Q7: How do create an array of generics, i.e., E[]?

The logical way to create an array containing items of type E (where E implements Comparable) would be to do something like:

E[] myArr = new E[INIT_SIZE];

However, Java does not allow you create a new array using generic type so what you'll need to do is create an array of Comparables and cast it to an array of items of type E:

E[] myArr = (E[])(new Comparable[INIT_SIZE]);
Last Updated: 8/16/2016     © 2014-2016 Beck Hasti and Charles Fischer