Permalink
Switch branches/tags
Nothing to show
Find file Copy path
96a4db0 Jul 21, 2016
1 contributor

Users who have contributed to this file

41 lines (32 sloc) 1001 Bytes
class MergeSort(object):
def __init__(self, numbers):
self.values = numbers
self.count = len(numbers)
def sort(self):
self.merge_sort(0, self.count - 1)
return self.values
def merge_sort(self, low, high):
if low < high:
mid = (low + high) // 2
self.merge_sort(low, mid)
self.merge_sort(mid + 1, high)
self.merge(low, mid, high)
def merge(self, low, mid, high):
b = []
i = low
j = mid + 1
while i <= mid and j <= high:
if self.values[i] <= self.values[j]:
b.append(self.values[i])
i += 1
else:
b.append(self.values[j])
j += 1
while i <= mid:
b.append(self.values[i])
i += 1
while j <= high:
b.append(self.values[j])
j += 1
for index, val in enumerate(b):
self.values[low + index] = val