Conduct doubling experiments to compute the run time and plot the results (for array A and B ) for array sizes n = 2, 4, ... 1024. The function merge_sort (provided) . def merge (front, back): pos_f, pos_b = 0,0 merged = np.zeros(len(front)+len(back)) for i in range (len(merged)): if pos_f == len(front): merged[i] = back[pos_b] pos_b += 1 elif pos_b == len(back): merged[i] = front[pos_f] pos_f += 1 elif front[pos_f] < back[pos_b]: merged[i] = front[pos_f] pos_f += 1 else: merged[i] = back[pos_b] pos_b += 1 return merged def merge_sort(A): n = len(A) if n <= 1: return A mid = int(n/2) front = merge_sort(A[0:mid]) back = merge_sort(A[mid:]) return merge(front, back) ARRAY A) Ai s perfectly sorted and contains random integers (0 to 100 inclusive). b) B is reversely sorted and contains random integers (0 to 100 inclusive)
Conduct doubling experiments to compute the run time and plot the results (for array A and B ) for array sizes n = 2, 4, ... 1024. The function merge_sort (provided) .
def merge (front, back):
pos_f, pos_b = 0,0
merged = np.zeros(len(front)+len(back))
for i in range (len(merged)):
if pos_f == len(front):
merged[i] = back[pos_b]
pos_b += 1
elif pos_b == len(back):
merged[i] = front[pos_f]
pos_f += 1
elif front[pos_f] < back[pos_b]:
merged[i] = front[pos_f]
pos_f += 1
else:
merged[i] = back[pos_b]
pos_b += 1
return merged
def merge_sort(A):
n = len(A)
if n <= 1:
return A
mid = int(n/2)
front = merge_sort(A[0:mid])
back = merge_sort(A[mid:])
return merge(front, back)
ARRAY
A)
b) B is reversely sorted and contains random integers (0 to 100 inclusive)
Step by step
Solved in 2 steps with 3 images