I have to modify a MergeSort method to sort a list of Comparable objects. I also have to modify the code to determine how many comparisons of items occur during the sorting process by incrementing a class level variable comparisons and then use the program to confirm the theoretical predictions of performance for Mergesort.
Here is the MergeSort Method -
I'm not sure of where to begin with this problem, and any help would be appreciated. Thanks.
Somewhere in your code above you are comparing two objects to find out which one is "smaller" than the other one. You want to count the number of comparisons. There is exactly 1 line in the mergeSort(...) method where the comparison is done, so there you simply have to increment the counter.
Look hard, make sure you understand the code and find the line where the comparison is done.
Good, you're on the right track. I see you've found the line of code where the comparison is done already.
I guess your class Account implements interface Comparable, so the implementation of the compareTo method is present in the Account class.
Your mergeSort() method now takes an array of ints:
If you want it to work on objects that implement the Comparable interface, you'll have to change it to take an array of Comparable objects:
Ofcourse you'll have to change other parts of the mergeSort() method also to make it work with an array of Comparable objects instead of an int array.
Instead of comparing two ints with the < operator, you call the compareTo() method to compare two Comparable objects. The API documentation of the compareTo() method in the Comparable interface describes the possible return values: < 0 if the object you're calling the method on is less than the object you're comparing it with, = 0 if they are equal, > 0 if the object you're calling the method on is greater than the object you're comparing it with.
Ok, i have completed this piece of code and it compiles and runs -
I now have the following which i need to do - Create an array of Comparable Account objects in account number order (or use the output from the above code i have already completed) then implement a recursive Binary search for an item.
I'm not sure where to get started on this question. Any help would be appreciated. Thanks.
I have tried to get this started and have the following done so far but don't know if i am going in the right direction. If anyone could help that would be great.
A "dutch baby" is not a baby. But this tiny ad is baby sized: