Please help me with these Java questions. Thank you very much 1/ Write a merge m
ID: 3698951 • Letter: P
Question
Please help me with these Java questions. Thank you very much
1/ Write a merge method that merges two array-based sorted lists together.
This static method is written from the implementation perspective, meaning it goes inside the SortedArrayList class.
The method should be efficient- meaning O(n).
This will require you to directly access both arrays.
This means you need to avoid the "shifting" that can occur during adds!
The method header is:
public static <U> SortedArrayList<U> createMergeList(SortedArrayList<U> listA, SortedArrayList<U> listB)
U here is the generic type. (What we usually call "T".)
You can assume U is Comparable.
Don't worry about why the generics looks a little different in this example. That is beyond the scope of what you are required to know for our course.
(The reason it's different is because we are declaring a generic type only for the current method.)
2.
Write a recursive, linear/sequential search that takes advantage of an array being sorted.
Optimize by stopping the search either if you reach the end of the array or you know that the element cannot be in the array.
Note: this is a linear search- not a binary search!
The data is sorted.
The method header is:
public boolean searchRecursiveLinearSorted(Comparable[] array, Comparable target)
3/
sortedList is an object of type SortedListInterface that holds Strings. What is the contents of sortedList after each statement below?
sortedList.add("ponies"); // a. what is the contents of the list?
sortedList.add("apples"); // b. what is the contents of the list?
sortedList.add("fruit"); // c. what is the contents of the list?
sortedList.add("bananas"); // d. what is the contents of the list?
sortedList.add("dogs"); // e. what is the contents of the list?
System.out.println(sortedList.getLength()); // f. what is the output?
System.out.println(sortedList.getPosition("bananas")); // g. what is the output?
System.out.println(sortedList.getPosition("elephant")); // h. what is the output?
sortedList.remove("bananas"); // i. what is the contents of the list?
Explanation / Answer
Please find my implemetation of Q1.
Please repost other in separate post.
Hi friend, Since you have not posted SortedList so i can not test.
I have implemented after assuming some function existence.
public static <U> SortedArrayList<U> createMergeList(SortedArrayList<U> listA, SortedArrayList<U> listB) {
SortedArrayList<U> mergedList = new SortedArrayList<U>();
int i=0, j=0;
int n1 = listA.size();
int n2 = listB.size();
while(i < n1 && j < n2) {
if(listA.get(i).compareTo(listB.get(j)) <= 0) {
mergedList.add(listA.get(i));
i++;
}else{
mergedList.add(listB.get(j));
j++;
}
}
// remaining elements form A
while(i < n1) {
mergedList.add(listA.get(i));
i++;
}
// remaining elements form B
while(j < n2) {
mergedList.add(listB.get(j));
j++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.