random.randint(1, max_val) for i in range(random.randint(min_length, max_length))) def insertion_sort(input_arr): array = copy.deepcopy(input_arr) for i in range(len(array)): j = i - 1 while j >= 0 and array[j] > array[j + 1]: array[j + 1], array[j] = array[j], array[j + 1] j -= 1 if display_visuals: motion.update_image(array, j) return array if display_visuals: print("\nEnter your display resolution:") resolution = [int(input("Width: ")), int(input("Height: "))] motion = Visualizer(resolution[0], resolution[1], max_val, 0) motion.animation_loop(insertion_sort, input_array) output_array = insertion_sort(input_array) print("Unsorted input: %s" % input_array) print("Sorted output: %s" % output_array)
random.randint(1, max_val) for i in range(random.randint(min_length, max_length))) def bubble_sort(input_arr): array = copy.deepcopy(input_arr) z = 1 while z < len(array): for i in range(len(array) - z): if array[i] > array[i + 1]: array[i], array[i + 1] = array[i + 1], array[i] if display_visuals: motion.update_image(array, i) z += 1 return array if display_visuals: print("\nEnter your display resolution:") resolution = [int(input("Width: ")), int(input("Height: "))] motion = Visualizer(resolution[0], resolution[1], max_val, 0) motion.animation_loop(bubble_sort, input_array) output_array = bubble_sort(input_array) print("Unsorted input: %s" % input_array) print("Sorted output: %s" % output_array)
if largest != i: arr[i], arr[largest] = arr[largest], arr[i] # swap if display_visuals: motion.update_image(arr, i) heapify(arr, n, largest) def heap_sort(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i) for i in range(n - 1, 0, -1): arr[i], arr[0] = arr[0], arr[i] heapify(arr, i, 0) if display_visuals: print("\nEnter your display resolution:") resolution = [int(input("Width: ")), int(input("Height: "))] motion = Visualizer(resolution[0], resolution[1], max_val, 0) motion.animation_loop(heap_sort, input_array) print("Unsorted input: %s" % input_array) heap_sort(input_array) print("Sorted output: %s" % input_array)