Is our linked-list-based implementation of merge-sort (Code Fragment 12.3) stable? Explain why or why not. /** Merge
Question:
Transcribed Image Text:
/** Merge contents of sorted queues S1 and S2 into empty queue S. */ public static
/** Merge contents of sorted queues S1 and S2 into empty queue S. */ public static void merge(Queue S1, Queue S2, Queue S, Comparator comp) { 3 while (!S1.isEmpty() && !S2.isEmpty()) { if (comp.compare(S1.first(), S2.first()) < 0) S.enqueue(S1.dequeue( )); else 4 // take next element from S1 6. // take next element from S2 S.enqueue(S2.dequeue( )); } while (!S1.isEmpty()) S.enqueue(S1.dequeue()); while (!S2.isEmpty()) S.enqueue(S2.dequeue( )); } 10 // move any elements that remain in S1 11 12 // move any elements that remain in S2 13 14 15 /** Merge-sort contents of queue. */ public static void mergeSort(Queue S, Comparator comp) { int n = S.size(); if (n < 2) return; // divide Queue S1 = new LinkedQueue<>(); Queue S2 = new LinkedQueue<>(); while (S1.size() < n/2) S1.enqueue(S.dequeue()); while (!S.isEmpty()) S2.enqueue(S.dequeue( )); // conquer (with recursion) mergeSort(S1, comp); mergeSort(S2, comp); // merge results merge(S1, S2, S, comp); } 16 17 18 // queue is trivially sorted 19 20 // (or any queue implementation) 21 22 23 // move the first n/2 elements to S1 24 25 // move remaining elements to S2 26 27 // sort first half // sort second half 28 29 30 // merge sorted halves back into original 31 32
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 85% (14 reviews)
It is not stable Given equ...View the full answer
Answered By
YOGENDRA NAILWAL
As I'm a Ph.D. student, so I'm more focussed on my chemistry laboratory. I have qualified two national level exams viz, GATE, and NET JRF (Rank 68). So I'm highly qualified in chemistry subject. Also, I have two years of teaching experience in this subject, which includes college teacher as well as a personal tutor. I can assure you if you hire me on this particular subject, you are never going to regret it.
Best Regards.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Question Posted:
Students also viewed these Computer science questions
-
Is our array-based implementation of merge-sort given in Section 12.1.2 stable? Explain why or why not.
-
Consider lines 3133 of Code Fragment 10.8 in our implementation of the class ChainHashMap. We use the difference in the size of a secondary bucket before and after a call to bucket.remove(k) to...
-
Consider the implementation of CircularlyLinkedList.addFirst, in Code Fragment 3.16. The else body at lines 39 and 40 of that method relies on a locally declared variable, newest. Redesign that...
-
Consider the problem of making change for n cents using the fewest number of coins. Assume that each coin's value is an integer. 2. Suppose that the available coins are in the denominations that are...
-
The following income statement items appeared on the adjusted trial balance of Schembri Manufacturing Corporation for the year ended December 31, 2018 ($ in thousands): sales revenue, $15,300; cost...
-
Sunset Trails, Inc., provides private recreational facilities, entertainment, and catering for large corporate groups, conventions, and other private parties. On March 25, 1996, Nortex Drug...
-
How can an employee be well organized yet unproductive? LO.1
-
If D = 8,000 per month, S = $45 per order, and H = $2 per unit per month, (a) What is the economic order quantity? (b) How does your answer change if the holding cost doubles? (c) What if the holding...
-
Open a ledger account for Cash in balance column format. Post general journal entries that impact cash from Exercise 2-9 to the ledger account for Cash, and enter the balance after each posting. Aug....
-
Assuming the cost of an associate leaving within 90 days is $3,000, what will be your facility's approximate cost of early turnover for this year? Year-to-Date Turnover Avg. Head- count Total < 90...
-
Suppose we are given two n-element sorted sequences A and B each with distinct elements, but potentially some elements that are in both sequences. Describe an O(n)-time method for computing a...
-
Give a complete justification of Proposition 12.1.
-
During the year, Bernard Company had net credit sales of $45,000. At the end of the year, before adjusting entries, the balance in Accounts Receivable was $12,500 (debit) and the balance in Allowance...
-
What's your favourite way to promote and build interpersonalwellness? How do you nurture your relationships? How do you build friendships? How do you show your loved ones that you care? Share your...
-
What is the best way to summary this formation? IMC Communication Objectives Increase from 0% to 75% of Millennials in the Dallas-Fort Worth area, ages 22 - 34, with a four-year college degree,...
-
Branding is very important to marketing, that is while most businesses focus on brands that attract customer's interest. Store brand is unique to a particular store compared to national brand . A...
-
Do you think that your score accurately reflects your global mindset? Why or why not? What, if anything, is missing from the assessment? How do you think that having a higher global mindset will help...
-
Healthcare is an ever-changing industry that requires healthcare organizations to align with those changes or risk being left behind. With the advances being made in technology, every corner seems to...
-
In the lithium atom (Z = 3) two electrons are in the n = 1 orbit and the third is in the n = 2 orbit. (Only two are allowed in the n = 1 orbit because of the exclusion principle, which will be...
-
Walker, Inc., is an all-equity firm. The cost of the company's equity is currently 11.4 percent and the risk-free.rate is 3.3 percent. The company is currently considering a project that will cost...
-
Prove that if a | b and b | c, then a | c.
-
How many steps would you expect POLLARD-RHO to require to discover a factor of the form p e , where p is prime and e > 1?
-
Prove that if x is a nontrivial square root of 1, modulo n, then gcd (x 1, n) and gcd (x + 1, n) are both nontrivial divisors of n.
-
A first-time shareholder has approached you requesting some advice. The shareholder has received the company's annual report and noticed the following statement in the summary of significant...
-
View Policies Current Attempt in Progress REI sells snowboards. Assume the following information relates to REI's purchases of snowboards during September. During the same month, 1 0 2 snowboards...
-
*Please explain how you got the answers* The following costs result from the production and sale of 1,000 drum sets manufactured by Tight Drums Company for the year ended December 31, 2019. The drum...
Study smarter with the SolutionInn App