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: 84% (13 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...
-
In rabbits, the dominant allele B causes black fur and the recessive allele b causes brown fur; for an independently assorting gene, the dominant allele R causes long fur and the recessive allele r...
-
We have seen that we can use a bubble plot to show a third quantitative variable on a scatterplot. Another way to show three quantitative variables together is to use a 3-dimensional scatterplot,...
-
Applying the net present value approach with and without tax considerations Luther Currie, the president of Luther's Moving Services, Inc., is planning to spend $625,000 for new trucks. He expects...
-
Image transcription text YAW Dynamics: Char. Velocity = 32 mph or 14 m/s Estimated Front mu = 0.55 Max Speed 130R = 59 mph Roll Dynamics: SSF = 69in / 56.2in = 1.22 Rear Ride Rate = 320lbs/2/ 0.5in =...
-
The Blending Department of Luongo Company has the following cost and production data for the month of April. Units transferred out totaled 20,740. Ending work in process was 1,220 units that are 100%...
-
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.
-
In Problems 32 through 36, writein the manner of Eqs. (3) through (6) of this sectiona differential equation that is a mathematical model of the situation described. In a city having a fixed...
-
Table 3.22 is a routing table using CIDR. Address bytes are in hexadecimal. The notation /12 in C4.50.0.0/12 denotes a netmask with 12 leading 1 bits, that is, FF.F0.0.0. Note that the last three...
-
Consider the sliding window algorithm with SWS = RWS = 3, with no out-of-order arrivals, and with infinite-precision sequence numbers. (a) Show that if DATA[6] is in the receive window, then DATA[0]...
-
In Problems 25-40, decide on a reasonable means for conducting the survey to obtain the desired information. A pet store would like to use a survey to determine which factors are most important to...
-
Suppose two subnets share the same physical LAN; hosts on each subnet will see the other subnets broadcast packets. (a) How will DHCP fare if two servers, one for each subnet, coexist on the shared...
-
Suppose hosts A and B are connected by a link. Host A continuously transmits the current time from a high-precision clock, at a regular rate, fast enough to consume all the available bandwidth. Host...
-
The objects being studied by a researcher are called ____.
-
Suppose the S&P 500 futures price is 1000, = 30%, r = 5%, = 5%, T = 1, and n = 3. a. What are the prices of European calls and puts for K = $1000? Why do you find the prices to be equal? b. What...
-
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.
-
Image transcription text Wind tunnel measurements of the pressure and skin friction around a NACA 2415 airfoil at 8 degrees angle of attack resulted in the following data of pressure and skin...
-
Image transcription text The following table contains load-extension data from a tensile test on a cylindrical specimen with gauge length 9mm and gauge diameter 5mm. Load-extension Data Load [KN] 0...
-
Image transcription text Systems Modelling and Analysis - Assignment 1 Due: Friday 25/08/2022 by 5:00:00 pm. To be submitted individually on Canvas and Gradescope. Part 1: Dartboard Positioning...
Study smarter with the SolutionInn App