Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the problem given: Add a method public Sequence mergeSorted(Sequence other) to the Sequence class of Exercise E7.20 that merges two sorted sequences, producing

Here is the problem given:

Add a method public Sequence mergeSorted(Sequence other) to the Sequence class of Exercise E7.20 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.

And here is the code I got from the website

public Sequence mergeSort(Sequence other) { Sequence sorted = new Sequence(); int valueIndex = 0; int otherIndex = 0; Sequence newSequence = new Sequence(); while(valueIndex < values.size() && otherIndex < other.values.size()) { if(values.get(valueIndex) < other.values.get(otherIndex)) { newSequence.set(values.get(valueIndex)); valueIndex = valueIndex + 1; } else { newSequence.set(other.values.get(otherIndex)); otherIndex = otherIndex + 1; } } while (valueIndex

public static void main(String[] args){ Sequence s1 = new Sequence(); s1.add(5); s1.add(8); s1.add(9) Sequence s2 = new Sequence(); s2.add(4); s2.add(5); s2.add(9); s2.add(15); System.out.println("Sequence: " + s1.toString()); System.out.println("Other sequence " + s2.toString()); System.out.println("The resultant sequence "); Sequence newseq = s1.mergeSort(s2); System.out.println(newseq.toString());

Works great, but I don't understand it. Can you please comment it out and provide a breif explantion of whats going on.

Thanksl

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago