Question

Using Python, remove the slice operator from the mergeSort function. The new code will look a...

Using Python, remove the slice operator from the mergeSort function. The new code will look a lot like the provided code except that you will be merging from the list being sorted into a temporary list. So take some time to understand exactly what the code in the book is doing before using it as a template for what you will need to add to the new function.

def mergeSort(alist, workspace=None, start=None, end=None):

#### You can change this code if you want to, but it sufficient to implement a solution.

if workspace is None:
workspace = [None] * len(alist)
start = 0
end = len(alist) #### Note that end is not a valid index into the array -- it's one past the last element!

if end-start <= 1:
return

midpoint = (start+end)//2

mergeSort(alist, workspace, start, midpoint)
mergeSort(alist, workspace, midpoint, end)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Python program

def merge(alist, workspace,l, m, r):
temp_pos=l
size=r-l+1
left_end=m-1
while l<=left_end and m<=r:
if alist[l]<alist[m]:
workspace[temp_pos]=alist[l]
l=l+1
else:
workspace[temp_pos]=alist[m]
m=m+1
temp_pos=temp_pos+1
while l<=left_end:
   workspace[temp_pos]=alist[l]
   l=l+1
   temp_pos = temp_pos+1
  
   while m<=r:
   workspace[temp_pos]=alist[m]
   m=m+1
   temp_pos = temp_pos+1
   i=0
  
   while i<size:
       alist[r]=workspace[r];
       r=r-1
       i=i+1
  
def mergeSort(alist,workspace,start,end):
if l < r:
mid = start+(end-l)/2
  
# Sort first and second halves
mergeSort(alist, workspace,start,mid)
mergeSort(alist, workspace,mid+1, end)
merge(alist, workspace,start, mid+1, end)

Add a comment
Know the answer?
Add Answer to:
Using Python, remove the slice operator from the mergeSort function. The new code will look a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the...

    Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the length and then calculate the average time to sort. Finally, compare the average run time for each length to the average time for the Merge Sort. -------------------------------------------- Initial python code: import random import time def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • Use python to code! Q1 - You need to keep track of book titles. Write code...

    Use python to code! Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list....

  • Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...

    Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here

  • 1. The Operand Stack - opstack The operand stack should be implemented as a Python list....

    1. The Operand Stack - opstack The operand stack should be implemented as a Python list. The list will contain Python integers, strings, and later in Part 2 code arrays. Python integers and lists on the stack represent Postscript integer constants and array constants. Python strings which start with a slash / on the stack represent names of Postscript variables. When using a list as a stack, assume that the top of the stack is the end of the list...

  • Finish each function python 3 LList.py class node(object): """ A version of the Node class with...

    Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class.    Since there are no setters and getters, we use the attributes directly.    This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...

  • python 3.6 please ! and comment your code .def remove(val, xs, limit-None): Remove multiple copies of...

    python 3.6 please ! and comment your code .def remove(val, xs, limit-None): Remove multiple copies of val from xs (directly modify the list value that xs refers to). You may only remove up to the first limit occurrences of val. If limit -3, and xs had ten copies of val in it, then you'd only remove the first three and leave the last seven in place. When limitNone, there's truly no limit (and we remove all occurrences of val). Return...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • this is the function and here's what I did for this function but there is an...

    this is the function and here's what I did for this function but there is an error showing saying nonetype does not have type len() because math.ceil function (we cannot import any math..) for this function in python, if you could help me with this code. thanks gerer grodpugu def slice_list(1st: List(Any) ni int) -> List[List[Any]]: Return a list containing slices of <st> in order. Each slice is a List of size <n> containing the next <n> elements in <tst>....

  • Please code in Python: A function can be assigned to a variable and passed into another...

    Please code in Python: A function can be assigned to a variable and passed into another function as an argument. Consider the following function that executes a function twice: def exec_2(f): f() f() any function that is passed into exec_2() will be executed twice without parameters. Your task is to write a function exec_4(f) that takes in a function f as a parameter and executes the function f 4 times using the function exec_2. The function f in this case...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT