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:
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.
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:
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.
When calculating multicomponent distillation, why is it best to list the components in order of decreasing...
When calculating multicomponent distillation, why is it best to list the components in order of decreasing volatility?
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT