I have a question about this mergeSort function. Please help!!! /u/FrancoLi06 Python Education

I am having trouble because it is not sorting the array. I am a bit confused because it should be sorting. When the recursive occur, I don’t understand what happen to the sortedArr that is returned. I think maybe that is why it is not getting sorted. Please enlighten me..

Here is my code: def merge(arr1, arr2): arr3 = [] i, j= 0, 0 while i < len(arr1) and j < len(arr2): if int(arr1[i]) <= int(arr2[j]): arr3.append(arr1[i]) i+=1 else: arr3.append(arr2[j]) j+=1 arr3 += arr1[i:] arr3 += arr2[j:] return arr3

def mergeSort(arr): l = 1 h = len(arr) sortedArr = [] if (l < h): mid = (l + h) // 2 left = arr[:mid] right = arr[mid:] mergeSort(left) mergeSort(right) sortedArr = merge(left, right) return sortedArr

submitted by /u/FrancoLi06
[link] [comments]

​r/learnpython I am having trouble because it is not sorting the array. I am a bit confused because it should be sorting. When the recursive occur, I don’t understand what happen to the sortedArr that is returned. I think maybe that is why it is not getting sorted. Please enlighten me.. Here is my code: def merge(arr1, arr2): arr3 = [] i, j= 0, 0 while i < len(arr1) and j < len(arr2): if int(arr1[i]) <= int(arr2[j]): arr3.append(arr1[i]) i+=1 else: arr3.append(arr2[j]) j+=1 arr3 += arr1[i:] arr3 += arr2[j:] return arr3 def mergeSort(arr): l = 1 h = len(arr) sortedArr = [] if (l < h): mid = (l + h) // 2 left = arr[:mid] right = arr[mid:] mergeSort(left) mergeSort(right) sortedArr = merge(left, right) return sortedArr submitted by /u/FrancoLi06 [link] [comments] 

I am having trouble because it is not sorting the array. I am a bit confused because it should be sorting. When the recursive occur, I don’t understand what happen to the sortedArr that is returned. I think maybe that is why it is not getting sorted. Please enlighten me..

Here is my code: def merge(arr1, arr2): arr3 = [] i, j= 0, 0 while i < len(arr1) and j < len(arr2): if int(arr1[i]) <= int(arr2[j]): arr3.append(arr1[i]) i+=1 else: arr3.append(arr2[j]) j+=1 arr3 += arr1[i:] arr3 += arr2[j:] return arr3

def mergeSort(arr): l = 1 h = len(arr) sortedArr = [] if (l < h): mid = (l + h) // 2 left = arr[:mid] right = arr[mid:] mergeSort(left) mergeSort(right) sortedArr = merge(left, right) return sortedArr

submitted by /u/FrancoLi06
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *