def mergesort(list, first, last): print 'mergesort(', first, ',', last, ')' if first < last: mid = (first+last)/2 mergesort(list, first, mid) mergesort(list, mid+1, last) merge(list, first, mid, last) return def merge(list, first, mid, last): merged = [ ] idx1 = first idx2 = mid + 1 while True: if list[idx1] < list[idx2]: merged.append(list[idx1]) idx1 = idx1 + 1 else: merged.append(list[idx2]) idx2 = idx2 + 1 if idx1 > mid or idx2 > last: break while idx1 <= mid: merged.append(list[idx1]) idx1 = idx1 + 1 while idx2 <= last: merged.append(list[idx2]) idx2 = idx2 + 1 list[first:last+1] = merged def main(): list = input('Enter the list: ') mergesort(list, 0, len(list)-1) print list return # testing for merge (ran this first) # list = [ 1, 5, 6, 2, 3, 9 ] # merge(list, 0, 2, 5) # print list main()