Question

Mergesort combining phase: 24     S = []   25     while L != [] and R != []:   26       if head(L)...

Mergesort combining phase:

24     S = []  
25     while L != [] and R != []:  
26       if head(L) <= head(R):  
27         S = S + [head(L)]  
28         L = tail(L)  
29       else:  
30         S = S + [head(R)]  
31         R = tail(R)  
32     S = S + L + R  
33     return S

Find the run time of mergesort’s combining phase (lines 24–33). Do not find the run time for the rest of mergesort. Do not use O, Ω, or Θ. Your answer must define T(n), where n is the number of elements to be sorted.
      Assume that head, tail, list concatenation ‘+’, and list creation ‘[x]’, work in constant time. (This is probably not true in Python, but assume it anyway!) Like the example in Cormen’s text, also assume that the run times of lines 24, 25 ..., 33 are constants c₂₄, c₂₅ ..., c₃₃. All these constants are greater than or equal to 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

let us have a look at how the code is working

suppose we need to merge two lists L = [1.4.6] and R = [2,5,7]

so we do it as first take s = [ ], now the steps are:

  1. s = [1] , L = [4,6] , R = [2,5,7]
  2. s = [1,2] , L = [4,6] , R = [5,7]
  3. s = [1,2,4] , L = [6] , R = [5,7]
  4. s = [1,2,4,5] , L = [6] , R = [7]
  5. s = [1,2,4,5,6] , L = [ ] , R = [7]
  6. s = [1,2,4,5,6,7] , L = [ ] , R = [ ]

So if you intutively see it, you know it takes n times to merge the n elements.

Writing it in the form of T(n)

T(n) = T(n - k) + T(k) // assuming the two parts are of length k and n-k length

We decrement one from either of T(n-k) or T(k), randomly until either of it is finished.

  1. T(n) = T(n - k - 1) + T(k) + c1
  2. T(n) = T(n - k - 1) + T(k-1) + c1 + c2
    .
    .
    .
    .
  3. at the nth step T(n) = c1 + c2 + c3 + ...cn

so t(n) = n x constant, so the time complexity is O(n)

EDIT

line 24 - 1 time

line 25 - 31 will run n times in total, you cannot accurately say which one of if or else will run exactly for how many times, if the if part runs for k times, the else part will run for n-k times

line 32 and 33 will run 1 time each

if the answer helped you please upvote and if you have any doubts please comment i will surely help.

Add a comment
Know the answer?
Add Answer to:
Mergesort combining phase: 24     S = []   25     while L != [] and R != []:   26       if head(L)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 01 def head(Q):   02   return Q[0]      03 def tail(Q):   04   return Q[1:]      05 def mergesort(U):   06   if...

    01 def head(Q):   02   return Q[0]      03 def tail(Q):   04   return Q[1:]      05 def mergesort(U):   06   if U == [] or tail(U) == []:   07     return U   08   else:   09     L = []   10     R = []   11     while U != [] and tail(U) != []:   12       L = L + [head(U)]   13       U = tail(U)   14       R = R + [head(U)]   15       U = tail(U)   16     L = L + U   17     L = mergesort(L)   18     R = mergesort(R)   19     S = []   20     while L != [] and R != []:   21       if head(L)...

  • In Python! Search Exercise 11.A: Mergesort with a Comparator CS 1410 Background The sort algorithms we...

    In Python! Search Exercise 11.A: Mergesort with a Comparator CS 1410 Background The sort algorithms we have looked at in Module 8 have all sorted list elements in ascending because it compares elements with the less-than operator. For example, in our mergesort program, the comparison appears as follows: 18 L[1] R01 The effect of this comparison is that if L(i) is less than RC), then L[is considered to come before R[i] in the sorted result. Hence the ascending order of...

  • Answer 23, 24, and 25. thanks 23. If the input to the above system is a)2cus(4), what would be the output y0)? Use the equation below for the following problem(s): (cos(,)-1) y(t) = What is t...

    Answer 23, 24, and 25. thanks 23. If the input to the above system is a)2cus(4), what would be the output y0)? Use the equation below for the following problem(s): (cos(,)-1) y(t) = What is the Fourier Transform of the signal given above? 24. Use the equation(s) below to solve the following problem(s): tn-nne [2.5] Q otherwise Qctherwise what is the result of the convolution of the two signals shown above, x(n-fn]? a. (000019.5/3 6.5/3 33/20 1 0 0 b....

  • 느 - K L M N O Р Q R S B с D E F...

    느 - K L M N O Р Q R S B с D E F G H 4 A sample of households provided the following information about their Per Capita Income. 5 Use Excel to construct a percent frequency distribution table and graph. 6 Provide an appropriate label for each of the horizontal and vertical axes. 7 Make your first class "30,000-34,999" 8 Place the frequency distribution table and histogram within the highlighted box below. 9 Per Capita 10...

  • 26/ Only calculate the speed of the segment A, B, C suddenly you sawa s later...

    26/ Only calculate the speed of the segment A, B, C suddenly you sawa s later you heard the air is about 340 m/s us system can prop- stimate the time it Time, t (s) ur finger touches a FIGURE 2-25 Problem 23 per hour s tortoise, which r 1.2 minutes the flight in the same hat was the aver- val? 24. . The position of a particle as a function of time is given by x (-5 m/s)t +...

  • Find the power of the signal conponent of a P is the Fourice t of s,(0)(2.5 points) edf,where S.f density of pu nose (2.5 points) . Consider the RL circuit shown below. Assume that R-10 and L-IN....

    Find the power of the signal conponent of a P is the Fourice t of s,(0)(2.5 points) edf,where S.f density of pu nose (2.5 points) . Consider the RL circuit shown below. Assume that R-10 and L-IN. Hint : Use Parseval's relationship if necessary i(t) e. What is the input signal-o-oise (SNR,ratio, defined as: SNR, 1olog..C)as polnts d. Find the output power spectral density of noise N,00 N,( HP, where HU) is the frequency response of the circuit, and N,(n)...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

  • 7. r in During a major disaster, for example, a humicane, wrthquake is very important to...

    7. r in During a major disaster, for example, a humicane, wrthquake is very important to have tems on hand in a recent survey, only 40% of Americans had a three-day supply of b Suppose a sample of 20 Americans is selected at random. Let X be the number of day supply of bored water on hand com e g whethe household ency r Does the random variable have a binomial distribution not why not? What artthew of n and...

  • 25. You’re at the zoo and have a big red 3.20 L helium balloon. The barometric...

    25. You’re at the zoo and have a big red 3.20 L helium balloon. The barometric pressure is 790.0 mm Hg. Then you hear the roar of a lion. Startled, you accidently release the balloon. It flies away. By the time it reaches the clouds the atmospheric pressure is only 0.300 atm. What is the new volume of the balloon? a) 6.20 L b) 11.1 L c) 1.06 L d) 8.43 L 26. A gas sample is held at constant...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT