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 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).
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)
write a function that return a list of row numbers in matrix with removing the wrong...
write a function that return a list of row numbers in matrix with removing the wrong value. Ex: remove_row( matrix, wro) if matrix = [[5,2,8],[6,7,20],[10,25,9]] wro= 20 then output will be [1,2] Do not use any built in functions.
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT