Relative Content

Tag Archive for pythonalgorithmsortingdata-structures

Efficient Merge sort implementation in python

def merge(arr, l, m, r): merged_arr = [] i, j = 0, 0 while (i < len(arr[l:m])) and (j < len(arr[m:r])): if arr[l+i] < arr[m+j]: merged_arr.append(arr[l+i]) i += 1 elif arr[l+i] >= arr[m+j]: merged_arr.append(arr[m+j]) j += 1 if i == len(arr[l:m]): merged_arr.extend(arr[(m+j):r]) if j == len(arr[m:r]): merged_arr.extend(arr[(l+i):m]) arr[l:r] = merged_arr return def merge_sort(arr, l, r): […]