Question: I need to return the sortedArray and the counter as a pair, like sortedArray,counter. in mergeSortCount, but it has not worked. The counter variable is
I need to return the sortedArray and the counter as a pair, like sortedArray,counter. in mergeSortCount, but it has not worked. The counter variable is counting correctly. I just need to return the pair, sortedArray,counter. Python code counter = 0 def mergeCount(list1,list2): global counter i = j = 0 rv = [ ] while i < len(list1) and j < len(list2): if list1[i] < list2[j]: rv += [list1[i]] i += 1 counter += 1 else: rv += [list2[j]] j += 1 counter += 1 if i == len(list1): rv += (list2[j:]) else: rv += list1[i:] return rv def mergeSortCount(A): n = len(A) if n <= 1: return A left = mergeSortCount(A[:n//2]) right = mergeSortCount(A[n//2:]) sortedArray = mergeCount(left,right) return sortedArray #print(mergeSortCount([8,4,2,5,1,3,7,6])) ## Should print "([1,2,3,4,5,6,7,8], 16)"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
