From a4ca5668d6a49e764a2967405292b947774bdc6f Mon Sep 17 00:00:00 2001 From: ForeignGods Date: Tue, 3 May 2022 21:44:26 +0200 Subject: [PATCH] Create BubbleSort.py --- BubbleSort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 BubbleSort.py diff --git a/BubbleSort.py b/BubbleSort.py new file mode 100644 index 0000000..86538ee --- /dev/null +++ b/BubbleSort.py @@ -0,0 +1,27 @@ +import bpy +from random import randint +count = 100 +cubes=[] +i = 0 +while i < count: + x = randint(1,10) + cube = bpy.ops.mesh.primitive_cube_add(location=(x, 0, 0), scale=(0.25, 0.25, 0.25)) + i += 1 +for ob in bpy.data.objects: + z = randint(1,10) + if ob.type == 'MESH': + ob.scale.z = z + cubes.append(ob) +n = len(cubes) +for i in range(n): + already_sorted = True + for j in range(n - i - 1): + if cubes[j].scale.z > cubes[j + 1].scale.z: + cubes[j].location = (j, 0.0, 0.0) + cubes[j].keyframe_insert(data_path="location", frame=i) + cubes[j+1].location = (j-1, 0.0, 0.0) + cubes[j+1].keyframe_insert(data_path="location", frame=i) + cubes[j], cubes[j + 1] = cubes[j + 1], cubes[j] + already_sorted = False + if already_sorted: + break \ No newline at end of file