node = TreeNode(val) return node else: if val <= root.val: root.left_child = putin(root.left_child, val) else: root.right_child = putin(root.right_child, val) return root if __name__ == "__main__": res = [] start = time.time() for _ in xrange(1000): size = random.randint(1, 1000) nums = [random.randint(-10, 30) for _ in xrange(size)] root = None for x in nums: root = putin(root, x) sortlist = [] inorder(root, sortlist) res.append(test(sortlist)) end = time.time() print("%s Pass." % (res.count(True))) print("%s Fail." % (res.count(False))) print("Time : %s" % (end - start))
if i != largest: nums[i], nums[largest] = nums[largest], nums[i] heapify(nums, length, largest) def heapsort(nums): length = len(nums) for i in xrange(length / 2 + 1, -1, -1): heapify(nums, length, i) for i in xrange(length - 1, -1, -1): nums[0], nums[i] = nums[i], nums[0] heapify(nums, i, 0) if __name__ == "__main__": res = [] start = time.time() for _ in xrange(100): size = random.randint(1, 100) nums = [random.randint(-10, 20) for _ in xrange(size)] heapsort(nums) res.append(test(nums)) end = time.time() print("%s Pass." % (res.count(True))) print("%s Fail." % (res.count(False))) print("Time : %s" % (end - start))
i = j print "i=%s, j=%s" % (i, j) a = array[i] while i > start: if a >= array[i - 1]: break array[i] = array[i - 1] i -= 1 array[i] = a j += 1 def insert_sort(array): insert(array, 0, len(array) - 1) if __name__ == "__main__": array = [100 - i for i in range(100)] insert_sort(array) print array from sorttest import test test(insert_sort)
merge_sort(array, mid, length - length / 2) i = start j = mid b = [] while i < mid and j < start + length: if array[i] <= array[j]: b.append(array[i]) i += 1 else: b.append(array[j]) j += 1 while i < mid: b.append(array[i]) i += 1 while j < start + length: b.append(array[j]) j += 1 for k in range(length): array[start + k] = b[k] if __name__ == "__main__": a = [2, 3, 5, 2, 7, 3, 4, 18, 8, 10, 12, 9] sort(a) #insert_sort(a, 0, len(a)) import sorttest sorttest.test(sort)