I am trying to count inversion from a file using MergeSort, and my code seems to work for smaller files, but bigger files produce the wrong output. My code:
InversionCount.py:
count = 0
def merge_sort_inversions(numberlist):
if len(numberlist) < 2:
return numberlist
else:
mid = len(numberlist) // 2
lefthalf = numberlist[:mid]
righthalf = numberlist[mid:]
return sort_count_inversions(merge_sort_inversions(lefthalf), merge_sort_inversions(righthalf))
def sort_count_inversions(l, r):
result = []
i=0
j=0
global count
while(i < len(l) and j<len(r)):
if (r[j] > l[i]):
result.append(l[i])
i+= 1
elif(r[j] < l[i]):
result.append(r[j])
count += (len(l) - i)
j+=1
if(j>=len(r)):
result+=l
elif(i>=len(l)):
result+=r
print(count)
return result
Application.py:
import InversionCount
text_file = open('algorithms-1-test2.txt', 'r')
number_list = text_file.readlines()
number_list = list(map(int, number_list))
InversionCount.merge_sort_inversions(number_list)
The answer is: 2407905288 but the final printed count I get is: 22945388587258. I'd really appreciate any help on this, as I'm trying to learn algorithms on my own. Also, my issue happens with large files, so how would I debug an issue that only seems to occur with a very large input (I tried testing this with smaller inputs and it gives the right answers)? Thank you!