示例#1
0
 def testSort(self, func, lst1, lst2, name):
     print('--%s--' % (name))
     # unsorted list without duplicate values
     time1 = func(lst1)
     self.assertTrue(issorted(lst1))
     # unsorted list with duplicate values
     time2 = func(lst2)
     self.assertTrue(issorted(lst2))
     # sorted list without duplicate values
     time3 = func(lst1)
     self.assertTrue(issorted(lst1))
     # sorted list with duplicate values
     time4 = func(lst2)
     self.assertTrue(issorted(lst2))
     print('l1: %s ms, l2: %s ms, l1(sorted): %s ms, l2(sorted): %s ms.' %
           (time1, time2, time3, time4))
示例#2
0
    _helper(lst, start, mid)
    _helper(lst, mid, end)
    _merge(lst, start, mid, end)


def _merge(lst, start, mid, end):
    left_idx = start
    right_idx = mid
    tmp = []
    for i in range(end - start):
        if lst[left_idx] < lst[right_idx]:
            tmp.append(lst[left_idx])
            left_idx += 1
        else:
            tmp.append(lst[right_idx])
            right_idx += 1
        if left_idx >= mid:
            tmp += lst[right_idx:end]
            break
        elif right_idx >= end:
            tmp += lst[left_idx:mid]
            break
    lst[start:end] = tmp[:]


if __name__ == '__main__':
    size = 10
    lst = randomlist(size, duplicate=False)
    mergesort(lst)
    print(issorted(lst))