Question

In: Computer Science

python3: Given a list of numbers in order order, return a new list sorted in non-decreasing...

python3:

Given a list of numbers in order order, return a new list sorted in non-decreasing order, and leave the original list unchanged.

function only

def mergeSort(inputArray):

#code here

a = [3,4,6,1,21,11,9,8]

b = mergeSort(a)

print('original array is: ', a)

print('sorted array is: ', b)

Solutions

Expert Solution

Code:

def mergeSortNew(arr):
if len(arr) >1:
mid = len(arr)//2 #Finding middle position of the array
L = arr[:mid] # Dividing the array elements equally into L and R
R = arr[mid:]
  
mergeSortNew(L) # running mergesort on first half of the array
mergeSortNew(R) # running mergesort on second half of the array
  
i = j = k = 0
  
# merging both the arrays
while i < len(L) and j < len(R):
if L[i] < R[j]: #copy least element first
arr[k] = L[i]
i+=1
else:
arr[k] = R[j]
j+=1
k+=1
  
# adding left over elements
while i < len(L):
arr[k] = L[i]
i+=1
k+=1
  
while j < len(R):
arr[k] = R[j]
j+=1
k+=1

def mergeSort(ar):
res = []
res = res + ar
mergeSortNew(res)
return res
  
a = [3, 4, 6, 1, 21, 11, 9, 8]
b = mergeSort(a)
print('Original array is:', a)
print('sorted array is:', b)


Image version of code:

Output:


Related Solutions

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Given a list of numbers, could you build a Turing machine to sort them into non-decreasing...
Given a list of numbers, could you build a Turing machine to sort them into non-decreasing order? Non-decreasing order is essentially the same thing as increasing order, except that it recognizes that some of the numbers might be repeated. Explain why this is or why this is not possible. Do not try to give an algorithm or even a description of how the machine would work--just an explanation as to why it is or is not possible.
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
In Python, Given a list of numbers, return a list where all adjacent == elements have...
In Python, Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3,3,2,2,4] returns [1,2,3,2,4]. You may create a new list or modify the passed in list (set function does not work in this case).
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and...
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and stores them in a 1D array of size 15. Next, prompt the user for a number to search for in the array (target). Then, print the array. Next, search the array using a linear search – printing out each of the indices (or “indexes”) that are being examined until the algorithm either finds the target or doesn’t. Then, do the same thing for a...
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
If A is the matrix for projecting onto a plane S in R³, then the eigenvalues of A in non- decreasing order are:
If A is the matrix for projecting onto a plane S in R³, then the eigenvalues of A in non- decreasing order are:
Written in MIPS assembly language If given an array which is sorted in ascending order, as...
Written in MIPS assembly language If given an array which is sorted in ascending order, as well as its length and a target value. You are asked to find the index of this target value in the array using binary search algorithm. Here is an example: array 1, 4, 6, 8, 10, 12 length 6 target 8 2 In this case, the returned result should be assigned to be 3, as array[3] is equal to target. (Note: The target will...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of integers ia odd. Ex: if the input 2 3 4 8 11 -1(a negative indicates end), the output is 4. the maximum number of inputs for any test case should not exceed 9 positive values. If exceeded , output Too many inputs". Hint: Use an array of size 9. First read the data into array.Then,based in the number of items, find the middle item.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT