Question

def _merge(lst: list, start: int, mid: int, end: int) -> None: """Sort the items in lst[start:end]...

def _merge(lst: list, start: int, mid: int, end: int) -> None:
"""Sort the items in lst[start:end] in non-decreasing order.

Precondition: lst[start:mid] and lst[mid:end] are sorted.
"""
result = []
left = start
right = mid
while left < mid and right < end:
if lst[left] < lst[right]:
result.append(lst[left])
left += 1
else:
result.append(lst[right])
right += 1

# This replaces lst[start:end] with the correct sorted version.
lst[start:end] = result + lst[left:mid] + lst[right:end]

def find_runs(lst: list) -> List[Tuple[int, int]]:
"""Return a list of tuples indexing the runs of lst.

Precondition: lst is non-empty.

>>> find_runs([1, 4, 7, 10, 2, 5, 3, -1])
[(0, 4), (4, 6), (6, 7), (7, 8)]
>>> find_runs([0, 1, 2, 3, 4, 5])
[(0, 6)]
>>> find_runs([10, 4, -2, 1])
[(0, 1), (1, 2), (2, 4)]
"""
runs = []

# Keep track of the start and end points of a run.
run_start = 0
run_end = 1

def timsort(lst: list) -> None:
"""Sort <lst> in place.

>>> lst = []
>>> timsort(lst)
>>> lst
[]
>>> lst = [1]
>>> timsort(lst)
>>> lst
[1]
>>> lst = [1, 4, 7, 10, 2, 5, 3, -1]
>>> timsort(lst)
>>> lst
[-1, 1, 2, 3, 4, 5, 7, 10]
"""
runs = find_runs(lst)

help me with timsort plz

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

# PLEASE LIKE THE SOLUTION

# FEEL FREE TO DISCUSS IN COMMENT SECTION

# run command import numpy before defining function

# timsort function sort the list in place that means no extra variable or place is used only lst variable is used
# we use numpy and sort the list

import numpy

def timsort(lst: list) -> None:
   """Sort <lst> in place.

   >>> lst = []
   >>> timsort(lst)
   >>> lst
   []
   >>> lst = [1]
   >>> timsort(lst)
   >>> lst
   [1]
   >>> lst = [1, 4, 7, 10, 2, 5, 3, -1]
   >>> timsort(lst)
   >>> lst
   [-1, 1, 2, 3, 4, 5, 7, 10]."""
   # convert list to array
   lst = numpy.array(lst)
   # now sort lst using selection sort
   for i in range(0,len(lst)):
       for j in range(i+1,len(lst)):
           if(lst[i]>lst[j]):
               #swap
               temp = lst[i]
               lst[i] = lst[j]
               lst[j] = temp

   # now convert back to list
   lst = list(lst)

   return lst

// SAMPLE OUTPUT

>>> Ist = [] >>> Ist = timsort(lst) >>> Ist [] >>> Ist = [1] >>> Ist = timsort(lst) >>> Ist [1] >>> lst = [1, 4, 7, 10, 2, 5,

Add a comment
Know the answer?
Add Answer to:
def _merge(lst: list, start: int, mid: int, end: int) -> None: """Sort the items in lst[start:end]...
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
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
Active Questions
ADVERTISEMENT