Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have a Python question about Merge Sort. I have this code for merge sort and I'm trying to sort a text file with random
I have a Python question about Merge Sort. I have this code for merge sort and I'm trying to sort a text file with random strings but it gives an error( line 66, in merge if L[i] <= R[j]: TypeError: '<=' not supported between instances of 'str' and 'int') How do I fix this error?
import sys | |
def merge_sort(A): | |
merge_sort2(A, 0, len(A)-1) | |
def merge_sort2(A, first, last): | |
if first < last: | |
middle = (first + last)//2 | |
merge_sort2(A, first, middle) | |
merge_sort2(A, middle+1, last) | |
merge(A, first, middle, last) | |
def merge(A, first, middle, last): | |
L = A[first:middle+1] | |
R = A[middle+1:last+1] | |
L.append(sys.maxsize) | |
R.append(sys.maxsize) | |
i = j = 0 | |
for k in range (first, last+1): | |
if L[i] <= R[j]: | |
A[k] = L[i] | |
i += 1 | |
else: | |
A[k] = R[j] | |
j += 1 | |
A = [5,9,1,2,4,8,6,3,7] | |
print(A) | |
merge_sort(A) | |
print(A) |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started