I am getting a TypeError when I try to use recursion in my merge sort function. I am trying to return a tuple containing a list and a number. If only the list is returned, then my function is able to sort properly and return a sorted list, however.
My desired output:
([1,2,3,4,5,6,7,8,9,10,...],100)
My merge function takes a list and simple lambda expression to compare two numbers.
def merge_s(list_s, ordering):
if len(list_s) < 2:
return list_s, 100
result = []
mid = int(len(list_s) / 2)
y = merge_s(list_s[:mid], ordering)
z = merge_s(list_s[mid:], ordering)
i = 0
j = 0
first_item = ''
second_item = ''
while i < len(y) and j < len(z):
first_item = y[i]
second_item = z[j]
if ordering(second_item, first_item):
result.append(z[j])
j += 1
else:
result.append(y[i])
i += 1
result += y[i:]
result += z[j:]
return result, 100
My main function:
from random import shuffle
def main():
for i in range(10):
data = list(range(100))
shuffle(data)
comp = lambda a, b: a < b #my compare function
(sorted_data, _) = merge_s(data, comp)
test = (sorted_data,_)
print(test)
However, I am getting the error:
TypeError: '<' not supported between instances of 'int' and 'list'