Question: Add a method public Sequence mergeSorted(Sequence other) to the Sequence class of Exercise E7.23 that merges two sorted sequences, producing a new sorted sequence.

Add a method public Sequence mergeSorted(Sequence other) to the Sequence class of Exercise • E7.23 that merges two sorted sequences, producing a new sorted sequence. Keep an index into each sequence, indicating how much of it has been processed already. Each time, append the smallest unprocessed value from either sequence, then advance the index. For example, if a is 1 4 9 16 and b is 4 7 9 9 11 then a.mergeSorted

(b) returns the sequence 1 4 4 7 9 9 9 11 16 If a or b is not sorted, merge the longest prefixes of a and b that are sorted.

Data from exercise E 7.23

Consider the following class:
public class Sequence {
private ArrayList values;
public Sequence() { values = new ArrayList(); }
public void add(int n) { values.add(n); }
public String toString() { return values.toString(); }
}
Add a method public Sequence append(Sequence other)
that creates a new sequence, appending this and the other sequence, without modifying either sequence. For example, if a is 1 4 9 16 and b is the sequence 9 7 4 9 11 then the call a.append

(b) returns the sequence 1 4 9 16 9 7 4 9 11 without modifying a or b.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres how you can accomplish this task using Java Ill start by defining the Sequence class as it is ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!