visualsort

 1from util.module import *
 2from collections.abc import Callable
 3
 4def compare(a: int, b: int, nums: list) -> bool:
 5  """
 6  Compares two numbers and updates the current frame.
 7  
 8  :param a (int): The index of the first number in the array.
 9  :type a: int
10  :param b (int): The index of the second number in the array.
11  :type b: int
12  :param nums (list): The array of numbers.
13  :type nums: list
14  :return: True if the first number is greater than the second number, False otherwise.
15  :rtype: bool
16  :raises IndexError: If either of the indices is out of bounds.
17  """
18  if(a < 0 or a >= len(nums) or b < 0 or b >= len(nums)):
19    raise IndexError("Index out of bounds.")
20  
21  assign_color(a, "green")
22  assign_color(b, "red")
23  return nums[a] > nums[b]
24
25def swap(a: int, b: int, nums: list) -> list:
26  """
27  Swaps two numbers and updates the current frame.
28
29  
30  :param a (int): The index of the first number in the array.
31  :type a: int
32  :param b (int): The index of the second number in the array.
33  :type b: int
34  :param nums (list): The array of numbers.
35  :type nums: list
36  :return: The array of numbers after the swap.
37  :rtype: list
38  :raises IndexError: If either of the indices is out of bounds.
39  """
40  if(a < 0 or a >= len(nums) or b < 0 or b >= len(nums)):
41    raise IndexError("Index out of bounds.")
42  
43  tmp = nums[a]
44  nums[a] = nums[b]
45  nums[b] = tmp
46  add_clip(nums)
47  reset_colors()
48  return nums
49
50def render(algorithm: Callable, video_directory: str = "./", vidoe_name: str = "movie", fps: int = 50) -> None:
51  """
52  Renders the given algorithm to a video.
53
54  :param algorithm (function): The sorting algorithm (writen using the provided visualsort functions) that will be rendered.
55  :type algorithm: function
56  :param video_directory (str): The dirrectory where the video file will be saved.
57  :type video_directory: str
58  :param video_name (str): The diseried name of the video file.
59  :type video_name: str
60  :param fps (int): The frame rate of the video.
61  :type fps: int
62  :raises IndexError: If either of the indices is out of bounds.
63  """
64  nums = generate_numbers()
65  reset_colors()
66  
67  add_clip(nums)
68  
69  algorithm(nums)
70  
71  go_through(nums)
72
73  if not os.path.exists(video_directory):
74    os.mkdir(video_directory)
75
76  final_clip = ImageSequenceClip(clips, fps=fps)
77
78  final_clip.write_videofile(os.path.join(video_directory, vidoe_name + ".mp4"), fps=fps)
def compare(a: int, b: int, nums: list) -> bool:
 5def compare(a: int, b: int, nums: list) -> bool:
 6  """
 7  Compares two numbers and updates the current frame.
 8  
 9  :param a (int): The index of the first number in the array.
10  :type a: int
11  :param b (int): The index of the second number in the array.
12  :type b: int
13  :param nums (list): The array of numbers.
14  :type nums: list
15  :return: True if the first number is greater than the second number, False otherwise.
16  :rtype: bool
17  :raises IndexError: If either of the indices is out of bounds.
18  """
19  if(a < 0 or a >= len(nums) or b < 0 or b >= len(nums)):
20    raise IndexError("Index out of bounds.")
21  
22  assign_color(a, "green")
23  assign_color(b, "red")
24  return nums[a] > nums[b]

Compares two numbers and updates the current frame.

Parameters
  • a (int): The index of the first number in the array.
  • b (int): The index of the second number in the array.
  • nums (list): The array of numbers.
Returns

True if the first number is greater than the second number, False otherwise.

Raises
  • IndexError: If either of the indices is out of bounds.
def swap(a: int, b: int, nums: list) -> list:
26def swap(a: int, b: int, nums: list) -> list:
27  """
28  Swaps two numbers and updates the current frame.
29
30  
31  :param a (int): The index of the first number in the array.
32  :type a: int
33  :param b (int): The index of the second number in the array.
34  :type b: int
35  :param nums (list): The array of numbers.
36  :type nums: list
37  :return: The array of numbers after the swap.
38  :rtype: list
39  :raises IndexError: If either of the indices is out of bounds.
40  """
41  if(a < 0 or a >= len(nums) or b < 0 or b >= len(nums)):
42    raise IndexError("Index out of bounds.")
43  
44  tmp = nums[a]
45  nums[a] = nums[b]
46  nums[b] = tmp
47  add_clip(nums)
48  reset_colors()
49  return nums

Swaps two numbers and updates the current frame.

Parameters
  • a (int): The index of the first number in the array.
  • b (int): The index of the second number in the array.
  • nums (list): The array of numbers.
Returns

The array of numbers after the swap.

Raises
  • IndexError: If either of the indices is out of bounds.
def render( algorithm: Callable, video_directory: str = './', vidoe_name: str = 'movie', fps: int = 50) -> None:
51def render(algorithm: Callable, video_directory: str = "./", vidoe_name: str = "movie", fps: int = 50) -> None:
52  """
53  Renders the given algorithm to a video.
54
55  :param algorithm (function): The sorting algorithm (writen using the provided visualsort functions) that will be rendered.
56  :type algorithm: function
57  :param video_directory (str): The dirrectory where the video file will be saved.
58  :type video_directory: str
59  :param video_name (str): The diseried name of the video file.
60  :type video_name: str
61  :param fps (int): The frame rate of the video.
62  :type fps: int
63  :raises IndexError: If either of the indices is out of bounds.
64  """
65  nums = generate_numbers()
66  reset_colors()
67  
68  add_clip(nums)
69  
70  algorithm(nums)
71  
72  go_through(nums)
73
74  if not os.path.exists(video_directory):
75    os.mkdir(video_directory)
76
77  final_clip = ImageSequenceClip(clips, fps=fps)
78
79  final_clip.write_videofile(os.path.join(video_directory, vidoe_name + ".mp4"), fps=fps)

Renders the given algorithm to a video.

Parameters
  • algorithm (function): The sorting algorithm (writen using the provided visualsort functions) that will be rendered.
  • video_directory (str): The dirrectory where the video file will be saved.
  • video_name (str): The diseried name of the video file.
  • fps (int): The frame rate of the video.
Raises
  • IndexError: If either of the indices is out of bounds.