Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file1 = open("IntegerArray.txt")
- array = []
- count = 0
- for line in file1:
- if count == 10:
- break
- array.append(int(line))
- file1.close()
- def mergeSort_count(list1, invCounter):
- if len(list1)<=1:
- return list1, invCounter
- else:
- midPoint = len(list1)/2
- left, invCounterL = mergeSort_count(list1[:midPoint], invCounter)
- right, invCounterR = mergeSort_count(list1[midPoint:], invCounter)
- invCounter = invCounter + invCounterL + invCounterR
- return merge_count(left,right, invCounter)
- def merge_count(list1, list2, invCounter):
- output = []
- i = 0
- j = 0
- splitInvs = 0
- while i<len(list1) and j < len(list2):
- if list1[i] < list2[j]:
- output.append(list1[i])
- i += 1
- else:
- output.append(list2[j])
- j += 1
- splitInvs += len(list1)-i
- while i<len(list1):
- output.append(list1[i])
- i += 1
- while j<len(list2):
- output.append(list2[j])
- j += 1
- invCounter += splitInvs
- return output, invCounter
- def mc_wrapper(list1):
- invCounter = 0
- finlist, finalCount = mergeSort_count(list1, invCounter)
- return finalCount
- print mc_wrapper(array)
Advertisement
Add Comment
Please, Sign In to add comment