Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 2.2 KB

ex03.md

File metadata and controls

59 lines (40 loc) · 2.2 KB

Exercise 03

Part A

Write a class called OptimizedBubbleSort with the following method:

public <T> int sort(T[] array, Comparator<T> comparator, boolean optimized)

The method should sort the input array based on a given Comparator. The method should return the number of times the method Comparator#compare is called.

If optimized is true, then instead of a naive bubble sort, you should run a better version. For example, one fact that can be exploited is that, if the last swap is done at position i, then all elements after i are necessarily sorted. So, in the next iteration, you do not need to check elements after i.

Write a class called OptimizedBubbleSortTest in which you test your implementation. You need at least one test with the following input array ["c", "b", "a", "d", "e", "f"]. On such array, you need to verify that your optimized bubble sort version should do less than half the comparisons than a non-optimized version. Note: be careful to do not call sorting twice on the same array, but rather create a new one. Add enough unit tests until your reach 100% statement coverage.

Part B

Consider the class org.pg4200.ex03.GameUser. Write a class called GameUserComparator that does implement Comparator<GameUser>. A GameUser is "larger" if it has more points. If points are the same, should consider alphabetic sorting based on the user id.

Add a new test in OptimizedBubbleSortTest in which you sort an array of GameUser using GameUserComparator.

Part C

Develop a class called SortCheckerImp and that implements the interface org.pg4200.ex03.SortChecker. Develop a test class called SortCheckerImpTest that does extend org.pg4200.ex03.SortCheckerTestTemplate, in which you test your SortCheckerImp implementation. If it is correct, all tests should pass.

Part D

Study the source code of BubbleSort and InsertionSort. Once you think you fully understand them, write their implementation on paper (e.g., using a pen), without looking at the source code. Once done, compare what you wrote with the actual implementations.

Solutions

Solutions to this exercise can be found in the solutions module, under the org.pg4200.sol03 package.