def mergeSort(Data):
# result = []
# This location can't operate result.append(group1(pop(0)) at the while loop.
if len(Data) <= 1:
return Data
mid = len(Data) // 2
group1 = mergeSort(Data[:mid])
group2 = mergeSort(Data[mid:])
result = []
# Why this postion?
while group1 and group2:
if group1[0] < group2[0]:
result.append(group1.pop(0))
else:
result.append(group2.pop(0))
while group1:
result.append(group1.pop(0))
while group2:
result.append(group2.pop(0))
return result
Hello. I have a doubt about this code. I defined function for making merge sorting algorithm. But the empty list for inserting values couldn't working when I input forward. I don't under reason why this list should be input in front of the while loop. please let me know.
result.append(group1(pop(0))because when you call function, python don't know what isgroup1? Besides, I test a case array [3, 2, 1] and your algorithm print out [1, 3, 2]. If I change array to [3, 2, 1, 4], output is right [1, 2, 3, 4].