Question: def merge ( arr , start, mid, end ) : start 2 = mid + 1 if arr [ mid ] < = arr [
def mergearr start, mid, end:
start mid
if arrmid arrstart: # If the two subarrays are already sorted, no need to merge
return
while start mid and start end:
if arrstart arrstart:
start
else:
value arrstart
index start
# Shift elements in the first subarray to the right by one position
while index start:
arrindex arrindex
index
arrstart value # Insert the smaller value at the correct position
start
mid
start
def mergeSortarr:
n lenarr
currsize
while currsize n :
leftstart
while leftstart n :
# Find ending point of left subarray
mid minleftstart currsize n
# Find starting point of right subarray
rightend minleftstart currsize n
# Merge subarrays arrleftstart...mid and arrmidrightend
mergearr leftstart, mid, rightend
leftstart currsize
currsize
if namemain:
arr
mergeSortarr
printSorted 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
