Question: def merge ( arr , start, mid, end ) : start 2 = mid + 1 if arr [ mid ] < = arr [

def merge(arr, start, mid, end):
start2= mid +1
if arr[mid]<= arr[start2]: # If the two subarrays are already sorted, no need to merge
return
while start <= mid and start2<= end:
if arr[start]<= arr[start2]:
start +=1
else:
value = arr[start2]
index = start2
# Shift elements in the first subarray to the right by one position
while index != start:
arr[index]= arr[index -1]
index -=1
arr[start]= value # Insert the smaller value at the correct position
start +=1
mid +=1
start2+=1
def mergeSort(arr):
n = len(arr)
curr_size =1
while curr_size < n -1:
left_start =0
while left_start < n -1:
# Find ending point of left subarray
mid = min(left_start + curr_size -1, n -1)
# Find starting point of right subarray
right_end = min(left_start +2* curr_size -1, n -1)
# Merge subarrays arr[left_start...mid] and arr[mid+1...right_end]
merge(arr, left_start, mid, right_end)
left_start +=2* curr_size
curr_size *=2
if __name__=='__main__':
arr =[12,11,13,5,6,7]
mergeSort(arr)
print("Sorted array:", arr)
calculate the time complexity for the code above.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!