Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help add a local comparsion counter to the merge sort below def merge(numbers, i, j, k): merged_size = k - i + 1 merged_numbers =

help add a local comparsion counter to the merge sort below

def merge(numbers, i, j, k):


merged_size = k - i + 1

merged_numbers = []

for l in range(merged_size):

merged_numbers.append(0)


merge_pos = 0


left_pos = i

right_pos = j + 1


while left_pos <= j and right_pos <= k:

if numbers[left_pos] < numbers[right_pos]:

merged_numbers[merge_pos] = numbers[left_pos]

left_pos = left_pos + 1

else:

merged_numbers[merge_pos] = numbers[right_pos]

right_pos = right_pos + 1

merge_pos = merge_pos + 1


while left_pos <= j:

merged_numbers[merge_pos] = numbers[left_pos]

left_pos = left_pos + 1

merge_pos = merge_pos + 1


while right_pos <= k:

merged_numbers[merge_pos] = numbers[right_pos]

right_pos = right_pos + 1

merge_pos = merge_pos + 1


merge_pos = 0

while merge_pos < merged_size:

numbers[i + merge_pos] = merged_numbers[merge_pos]

merge_pos = merge_pos + 1



def merge_sort_recursive(numbers, i, k):

if i < k:

j = (i + k) // 2

merge_sort_recursive(numbers, i, j)

merge_sort_recursive(numbers, j + 1, k)

merge(numbers, i, j, k)


def merge_sort(numbers):

merge_sort_recursive(numbers, 0, len(numbers)-1)


Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions