Computer Science 210
Data Structures
Fall 2017, Siena College
This week, we will practice more with recursion, and experiment with searching and sorting.
You will be assigned a partner on this lab. Only one submission per group is needed.
Getting Set Up
You will receive an email with the link to follow to set up your GitHub repository searchingsorting-lab-yourgitname for this Lab. One member of the group should follow the link to set up the repository on GitHub, then that person should email the instructor with the other group members' GitHub usernames so they can be granted access. This will allow all members of the group to clone the repository and commit and push changes to the origin on GitHub. At least one group member should make a clone of the repository to begin work.
Please answer lab questions in the README.md file in your group's GitHub repository.
Recursion on Arrays
Your first task is to fill in the methods in RecursivePractice.java. Each is worth 10 points. You may test by writing a main method or by calling the methods within BlueJ and providing appropriate parameters.
No loops!
Recursive Call Trees
Consider the recursive method below for summing the values in an array from position first to last, inclusive. Below the method is the recursive call tree showing all the recursive calls made when the method is initially invoked with the array A containing values {5, 2, 4, 1, 6} and first = 0 and last = 4. The lines show the recursive calls made, and the number next to the line shows the value returned by the call.
public static int sum(int[] A, int first, int last) { if (first == last) { return A[first]; } else { int mid = (first + last) / 2; int left = sum(A, first, mid); int right = sum(A, mid+1, last); return left + right; } }
public static void mystery(String s, int d) { if (d == 0) { System.out.print(s + " "); } else { mystery(s + "0", d - 1); mystery(s + "1", d - 1); } }
Making Ratios Comparable
For the second part of the lab assignment, you will be seeing how we can create our own Comparable objects that could be used with generic code like the search methods in the BinSearch example. Both Ratio.java and BinSearch.java are in your repository. Put the names of all group members working on this task in those files now. Yes, do it now. Now. Really. Don't make me ask you again.
public class Ratio implements Comparable<Ratio>
This tells Java that this class can be treated as a "comparable" object like Integer, Double, String, and many others.
If you try to compile now (do it), you should get an error message something like
Ratio is not abstract and does not override abstract method compareTo(Ratio) in Comparable
This means that you said Ratio objects are comparable but didn't tell Java how to do that comparison. We do it in the same way that we compare other objects: by providing a compareTo method.
public int compareTo(Ratio other)
For now, have your method simply return 0 in all cases. You should now be able to compile Ratio.java again.
Your only experience so far with compareTo methods is probably the one built in to the String class. Recall that for String references s1 and s2, the call
s1.compareTo(s2)
will return 0 if s1.equals(s2); a value less than 0 if this s1 is lexicographically less than s2; and a value greater than 0 if s1 is lexicographically greater than s2.
For Ratio objects, we want to do the same thing: if we have two Ratio objects r1 and r2, the call
r1.compareTo(r2)
should return 0 if r1.equals(r2) (keep in mind this means that the 1/2 should be equal to 3/6); a value less than 0 if this r1 is less than r2; and a value greater than 0 if r1 is greater than r2.
It doesn't matter what positive or negative value you return, as long as it's positive when it should be, negative when it should be, and 0 when they're equal. (10 points)
Sorting
Read the topic notes about selection sort and insertion sort and look at the recursive implementations of those algorithms in the SortingComparisons example.
Submitting
Your submission requires that all required deliverables are committed and pushed to the master for your repository's origin on GitHub. That's it! If you see everything you intend to submit when you visit your repository's page on GitHub, you're set.
Grading
This assignment is worth 100 points, which are distributed as follows:
> Feature | Value | Score |
maximum method | 10 | |
count100 method | 10 | |
negatives method | 10 | |
prefixSum method | 10 | |
Q1: sum call tree | 5 | |
Q2: count100 call tree | 7 | |
Q3: mystery method trace | 8 | |
Ratio compareTo method | 10 | |
Ratio main method tests | 5 | |
BinSearch main method Ratio tests | 5 | |
selSort trace | 10 | |
insSort trace | 10 | |
Total | 100 | |