# Recursive merge sort code to sort in ascending order def mergeSort(myList): # print "Start of mergeSort", myList if len(myList)>1: middle = len(myList)/2 leftHalf = myList[:middle] rightHalf = myList[middle:] mergeSort(leftHalf) mergeSort(rightHalf) merge(myList, leftHalf, rightHalf) # print "End of mergeSort", myList def merge(sortedList, leftHalf, rightHalf): # both the leftHalf and rightHalf lists are sorted left = 0 right = 0 sortedIndex = 0 # loop until one list runs out while left < len(leftHalf) and right < len(rightHalf): if leftHalf[left] > rightHalf[right]: sortedList[sortedIndex] = rightHalf[right] right += 1 else: sortedList[sortedIndex] = leftHalf[left] left += 1 sortedIndex += 1 # copy the remaining items in the leftHalf while left < len(leftHalf): sortedList[sortedIndex] = leftHalf[left] left += 1 # copy the remaining items in the rightHalf while right < len(rightHalf): sortedList[sortedIndex] = rightHalf[right] right += 1