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
  • def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in...

    def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements in <lst>. === Precondition === n <= len(lst) >>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False],...

  • Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of...

    Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by their second position. (Modify the merge section to break ties) Modify in python def mergesort(mlist): if len(mlist) < 2: return mlist else: mid = len(mlist)//2 return merge( mergesort(mlist[:mid]), mergesort(mlist[mid:])) # merge two sorted lists def merge(left, right): if left == []: return right elif right == []: return left elif left[0] < right[0]: return [left[0]] + merge(left[1:], right)...

  • Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by...

    Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by their second position. (Modify the merge section to break ties) Modify in python def mergesort(mlist): if len(mlist) < 2: return mlist else: mid = len(mlist)//2 return merge( mergesort(mlist[:mid]), mergesort(mlist[mid:])) # merge two sorted lists def merge(left, right): if left == []: return right elif right == []: return left elif left[0] < right[0]: return [left[0]] + merge(left[1:], right)...

  • def get_indices(lst, elm): ''' (list of lists, object) -> tuple of two ints Given a list...

    def get_indices(lst, elm): ''' (list of lists, object) -> tuple of two ints Given a list of lists and an element, find the first pair of indices at which that element is found and return this as a tuple of two ints. The first int would be the index of the sublist where the element occurs, and the second int would be the index within this sublist where it occurs. >>> get_indices([[1, 3, 4], [5, 6, 7]], 1) (0, 0)...

  • 3 move_small_joker def move_small_joker(deck: List[int]) -> None: 'Swap the card with one follows it Precondition: card...

    3 move_small_joker def move_small_joker(deck: List[int]) -> None: 'Swap the card with one follows it Precondition: card is a non-negative integer I have a 3 here ! >>> deck = [1, 2, 4, 3] >>> move_small_joker(deck) >>> deck (3, 1, 2, 4 >>> deck = [1, 2, 3, 4] >>> move_small.joker(deck) >>> deck [1, 2, 4, 3] new = deck new.sort small_joker = new[-2] if deck.index(small_joker) == len(deck) -1: deck = [small_joker] + deck[O:-1] else: temp = deck[deck.index(small_joker)] deck[deck. index(small_joker)] =...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • def calculate_total(price_list: List[list]) -> int: """Return the sum of all second elements in the sublists of...

    def calculate_total(price_list: List[list]) -> int: """Return the sum of all second elements in the sublists of price_list. Precondition: price_list is a list of lists of length 2, and the second element of it sublist is an int. >>> price_list = [["apple", 1], ["sugar", 5], ["mango", 3], ... ["coffee", 9], ["trail mix", 6]] >>> calculate_total(price_list)

  • #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are...

    #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are two problems in #our current code: # - There's a missing line # - There's a semantic error (the code does not raise an # error message, but it does not perform correctly) # #Find and fix these problems! Note that you should only need #to change or add code where explicitly indicated. # #Hint: If you're stuck, use an example input list and...

  • 2. The mergesort is a recursive sort. What is the base case for the merge sort?...

    2. The mergesort is a recursive sort. What is the base case for the merge sort? A.There is none. B. When right = left -1 C. When start = end D. When end - start = 1 3. Given the array [ 23, 83, 82, 92, 28, 21, 91, 17, 54, 32, 41, 14], what is the value of mid for the call mergesort (elements, 0, 5, temp) ? A. The value of mid is 3 B. The value of...

  • When sorting using merge sort, I get confused about what going on. Consider the following array...

    When sorting using merge sort, I get confused about what going on. Consider the following array A = [7,5,2,8,9,1] and the following code: -------------------------------------------------------------------------------------------------------------------------- int merge_sort(int *input, int start, int end) { if (start < end) { int middle = (start + end ) / 2; merge_sort(input, start, middle); merge_sort(input, middle+1, end); merge(input, start, middle, end); } return 0; } int merge(int *input, int left, int middle, int right) { // determine lenghts int length1 = middle - left +...

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