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
# 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
def _merge(lst: list, start: int, mid: int, end: int) -> None: """Sort the items in lst[start:end]...
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 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 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 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. 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 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)
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...
Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1]; } void mergeSort(int arr[], int start, int end){ //base case: down to 1 item, do nothing //recursive case: //MergeSort(left) //MergeSort(right) //Merge(left, right) int m = (end - start) / 2; if(start==end){ } else { mergeSort(arr, start, m); mergeSort(arr, m+1, end); merge(arr, start, m, m+1, end); } } void fill(int arr[],...
Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...
7. Consider the following proposed sorting algorithm supersort (int n, int start, int end, keytype SI1)1 if(n > 1) { if (SIstart] > S[end]) swap SIstart] with Stend]; supersort(n-l, start, end-1, s) supersort (n-1, start+, end, S) a) 3 pts) Give a recurrence relation (with initial condition) that describes the complexity of this sort algorithm b) (4 pts) Solve the recurrence froma) c) (3 pts) Is supersort guaranteed to correctly sort the list of items? Justify your answer. (A formal...