Looping through 2D array and sorting each subarray using Merge Sort /u/Living_Cartoonist791 Python Education

I trying to apply the merge sort algorithm to each sub-array in arraytest using a for loop. However, I keep receiving an error saying ‘int’ object not iterable. I am confused because arraytest has a length of 3, and if you wanna check the first subarray, it will print subarray[0]. How do i fix my for statement loop in my code below?:

arraytest = [[3, 2, 6, 8], [5, 4, 2, 19], [97, 43, 23, 25]] def merge_sort(array): for j in len(array): if len(array[j]) <= 1: return array[j] midPoint = len(array[j]) // 2 leftArray = array[j][:midPoint] rightArray = array[j][midPoint:] leftArray = merge_sort(leftArray) rightArray = merge_sort(rightArray) l = 0 r = 0 finalOrder = [0] * len(array[j]) i = 0 while l < len(leftArray) and r < len(rightArray): if leftArray[l] < rightArray[r]: finalOrder[i] = leftArray[l] l +=1 elif rightArray[r] < leftArray[l]: finalOrder[i] = rightArray[r] r +=1 i +=1 while l < len(leftArray): finalOrder[i] = leftArray[l] l +=1 i += 1 while r < len(rightArray): finalOrder[i] = rightArray[r] r +=1 i += 1 return finalOrder merge_sort(arraytest) arraytest = [[3, 2, 6, 8], [5, 4, 2, 19], [97, 43, 23, 25]] def merge_sort(array): for j in len(array): if len(array[j]) <= 1: return array[j] midPoint = len(array[j]) // 2 leftArray = array[j][:midPoint] rightArray = array[j][midPoint:] leftArray = merge_sort(leftArray) rightArray = merge_sort(rightArray) l = 0 r = 0 finalOrder = [0] * len(array[j]) i = 0 while l < len(leftArray) and r < len(rightArray): if leftArray[l] < rightArray[r]: finalOrder[i] = leftArray[l] l +=1 elif rightArray[r] < leftArray[l]: finalOrder[i] = rightArray[r] r +=1 i +=1 while l < len(leftArray): finalOrder[i] = leftArray[l] l +=1 i += 1 while r < len(rightArray): finalOrder[i] = rightArray[r] r +=1 i += 1 return finalOrder merge_sort(arraytest) 

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

​r/learnpython I trying to apply the merge sort algorithm to each sub-array in arraytest using a for loop. However, I keep receiving an error saying ‘int’ object not iterable. I am confused because arraytest has a length of 3, and if you wanna check the first subarray, it will print subarray[0]. How do i fix my for statement loop in my code below?: arraytest = [[3, 2, 6, 8], [5, 4, 2, 19], [97, 43, 23, 25]] def merge_sort(array): for j in len(array): if len(array[j]) <= 1: return array[j] midPoint = len(array[j]) // 2 leftArray = array[j][:midPoint] rightArray = array[j][midPoint:] leftArray = merge_sort(leftArray) rightArray = merge_sort(rightArray) l = 0 r = 0 finalOrder = [0] * len(array[j]) i = 0 while l < len(leftArray) and r < len(rightArray): if leftArray[l] < rightArray[r]: finalOrder[i] = leftArray[l] l +=1 elif rightArray[r] < leftArray[l]: finalOrder[i] = rightArray[r] r +=1 i +=1 while l < len(leftArray): finalOrder[i] = leftArray[l] l +=1 i += 1 while r < len(rightArray): finalOrder[i] = rightArray[r] r +=1 i += 1 return finalOrder merge_sort(arraytest) arraytest = [[3, 2, 6, 8], [5, 4, 2, 19], [97, 43, 23, 25]] def merge_sort(array): for j in len(array): if len(array[j]) <= 1: return array[j] midPoint = len(array[j]) // 2 leftArray = array[j][:midPoint] rightArray = array[j][midPoint:] leftArray = merge_sort(leftArray) rightArray = merge_sort(rightArray) l = 0 r = 0 finalOrder = [0] * len(array[j]) i = 0 while l < len(leftArray) and r < len(rightArray): if leftArray[l] < rightArray[r]: finalOrder[i] = leftArray[l] l +=1 elif rightArray[r] < leftArray[l]: finalOrder[i] = rightArray[r] r +=1 i +=1 while l < len(leftArray): finalOrder[i] = leftArray[l] l +=1 i += 1 while r < len(rightArray): finalOrder[i] = rightArray[r] r +=1 i += 1 return finalOrder merge_sort(arraytest) submitted by /u/Living_Cartoonist791 [link] [comments] 

I trying to apply the merge sort algorithm to each sub-array in arraytest using a for loop. However, I keep receiving an error saying ‘int’ object not iterable. I am confused because arraytest has a length of 3, and if you wanna check the first subarray, it will print subarray[0]. How do i fix my for statement loop in my code below?:

arraytest = [[3, 2, 6, 8], [5, 4, 2, 19], [97, 43, 23, 25]] def merge_sort(array): for j in len(array): if len(array[j]) <= 1: return array[j] midPoint = len(array[j]) // 2 leftArray = array[j][:midPoint] rightArray = array[j][midPoint:] leftArray = merge_sort(leftArray) rightArray = merge_sort(rightArray) l = 0 r = 0 finalOrder = [0] * len(array[j]) i = 0 while l < len(leftArray) and r < len(rightArray): if leftArray[l] < rightArray[r]: finalOrder[i] = leftArray[l] l +=1 elif rightArray[r] < leftArray[l]: finalOrder[i] = rightArray[r] r +=1 i +=1 while l < len(leftArray): finalOrder[i] = leftArray[l] l +=1 i += 1 while r < len(rightArray): finalOrder[i] = rightArray[r] r +=1 i += 1 return finalOrder merge_sort(arraytest) arraytest = [[3, 2, 6, 8], [5, 4, 2, 19], [97, 43, 23, 25]] def merge_sort(array): for j in len(array): if len(array[j]) <= 1: return array[j] midPoint = len(array[j]) // 2 leftArray = array[j][:midPoint] rightArray = array[j][midPoint:] leftArray = merge_sort(leftArray) rightArray = merge_sort(rightArray) l = 0 r = 0 finalOrder = [0] * len(array[j]) i = 0 while l < len(leftArray) and r < len(rightArray): if leftArray[l] < rightArray[r]: finalOrder[i] = leftArray[l] l +=1 elif rightArray[r] < leftArray[l]: finalOrder[i] = rightArray[r] r +=1 i +=1 while l < len(leftArray): finalOrder[i] = leftArray[l] l +=1 i += 1 while r < len(rightArray): finalOrder[i] = rightArray[r] r +=1 i += 1 return finalOrder merge_sort(arraytest) 

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

Leave a Reply

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