Question: Objective: Implement some recursive algorithms - - most related to sorting. all methods you define should be marked static. Q 0 ( warmup ) :
Objective: Implement some recursive algorithms most related to sorting.
all methods you define should be marked static.
Qwarmup: reversing a generic list recursively
Write a recursive method named rev that is parameterized by a generic type A
The method should take a List as a formal parameter and return a List that's the same as the input list, but with everything in reverse order.
Some examples using as an abbreviation for List.of put these in the main and print the results:
revcot; returns t o c revnew ArrayList returns
Try to do this one without a helper method...
Note: since you'll need to mutate the input list which in many cases will be readonlyimmutable you will need a "kickoff" method to make a defensive mutable copy of the input list as it may get destroyedmutated during reversal.
For example:
public static Foo exampleMethodList inputList return realExampleMethodnew ArrayListinputList; private static Foo realExampleMethodList lst recursive code in here can now safely mutate lst
The exampleMethod copies its input list and passes the mutable copy to the other method realExampleMethod which does all the actual work.
Q: merging two sorted lists recursively
A key 'helper' method used in merge sorting algorithm is the merge method.
The merge method should be parameterized by an orderable generic type U that extends Comparable.
the method should take as parameters two sorted, orderablegeneric List: I'll call these lists xs and ys
the method should return a list of U
Here are some examples where is used as pseudocode for List.of print these in the main:
merge returns merge returns merge returns returns a b c d e f g mergebdefacg SHOULDN'T WORK: merge method contract violation one of the input lists isn't sorted merge returns
tester.java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Tester
todo: static rev method here
todo: static merge method here
public static void mainString args
todo: sample calling code for q here
System.out.println;
todo: sample calling code for q here
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
