-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20230916_bubble_sort_algorithm.py
190 lines (168 loc) · 9.3 KB
/
20230916_bubble_sort_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import numpy
from manim import *
from numpy import *
config.pixel_width = 180 * 2
config.pixel_height = 320 * 2
class BubbleSort(Scene):
def construct(self):
self.add(Text("Bubble Sort Algorithm", font_size=64).move_to([0, 9, 0]))
self.add(Rectangle(width=9.5, height=1.8, stroke_color=BLACK,
fill_color=GREEN, fill_opacity=0.4).move_to([0, 9, 0]))
groups = []
j = 6.5
scale = 0.8
list = [10, 8, 2, 9, 1, 6, 3, 5, 7, 4]
for i in list:
group = VGroup(Rectangle(width=i, height=1, fill_color=BLUE, fill_opacity=0.4)
.move_to([-4 + i / 2.0, j, 0]),
Text(str(i)).move_to([-4 + i / 2.0, j, 0])
)
groups.append(group)
j = j - 1.5
self.add(*groups)
# self.play(groups[len(groups) - 1].animate.set_fill(RED))
brace = BraceBetweenPoints(
[-4, 7, 0], [-4, 4.5, 0], color=PURPLE, sharpness=2.0, stroke_width=10
)
txt = Text(r"First Round", font_size=200, color=RED, stroke_width=5)
self.play(FadeIn(brace), GrowFromCenter(txt))
self.remove(txt)
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(8)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(2)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(9)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(1)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(6)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(3)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(5)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(7)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(10)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(4)].animate.shift(numpy.array([0, +1.5, 0]))
)
txt = Text(r"Second Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 8, 0])))
self.play(groups[list.index(8)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(2)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(9)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(1)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(9)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(6)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(9)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(3)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(9)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(5)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(9)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(7)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(9)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(4)].animate.shift(numpy.array([0, +1.5, 0]))
)
txt = Text(r"Third Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 7, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(8)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(1)].animate.shift(numpy.array([0, +1.5, 0]))
)
for val in [6, 3, 5, 7, 4]:
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(8)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(val)].animate.shift(numpy.array([0, +1.5, 0]))
)
txt = Text(r"Forth Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 6, 0])))
self.play(groups[list.index(2)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(1)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(6)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(3)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(6)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(5)].animate.shift(numpy.array([0, +1.5, 0]))
)
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(7)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(4)].animate.shift(numpy.array([0, +1.5, 0]))
)
txt = Text(r"Fifth Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(6)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(4)].animate.shift(numpy.array([0, +1.5, 0]))
)
txt = Text(r"Sixth Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 4, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(groups[list.index(5)].animate.shift(numpy.array([0, -1.5, 0])),
groups[list.index(4)].animate.shift(numpy.array([0, +1.5, 0]))
)
txt = Text(r"Seventh Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 3, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
txt = Text(r"Eighth Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 2, 0])))
self.play(brace.animate.shift(numpy.array([0, -1.5, 0])))
txt = Text(r"Ninth Round", font_size=200, color=RED, stroke_width=5)
self.play(GrowFromCenter(txt))
self.remove(txt)
self.play(brace.animate.shift(numpy.array([0, 1.5 * 1, 0])))
self.remove(brace)
self.wait(3)