Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions