values of counter objects start at 0
This commit is contained in:
@@ -7,33 +7,35 @@ from mathutils import Vector, Matrix
|
|||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def bubble_sort(cubes, arrayCounter, comparisonCounter):
|
def bubble_sort(cubes, arrayCounter, comparisonCounter):
|
||||||
|
|
||||||
for i in range(len(cubes)-1):
|
for i in range(len(cubes)-1):
|
||||||
|
|
||||||
#insert keyframe for every cube on every frame
|
#insert keyframe for every cube on every frame
|
||||||
for cube in cubes:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=i)
|
cube.keyframe_insert(data_path="location", frame=i+1)
|
||||||
|
|
||||||
already_sorted = True
|
already_sorted = True
|
||||||
for j in range(len(cubes) - i -1):
|
for j in range(len(cubes) - i -1):
|
||||||
|
|
||||||
#add 1 to comparison counter
|
#add 1 to comparison counter
|
||||||
comparisonCounter.inputs[0].default_value += 1
|
comparisonCounter.inputs[0].default_value += 1
|
||||||
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i+1)
|
||||||
|
|
||||||
#add 2 to array counter
|
#add 2 to array counter
|
||||||
arrayCounter.inputs[0].default_value += 2
|
arrayCounter.inputs[0].default_value += 2
|
||||||
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i+1)
|
||||||
|
|
||||||
if cubes[j].scale.z > cubes[j + 1].scale.z:
|
if cubes[j].scale.z > cubes[j + 1].scale.z:
|
||||||
|
|
||||||
#change location & insert keyframes based on bubble sort
|
#change location & insert keyframes based on bubble sort
|
||||||
cubes[j].location.x = j
|
cubes[j].location.x = j
|
||||||
cubes[j].keyframe_insert(data_path="location", frame=i)
|
cubes[j].keyframe_insert(data_path="location", frame=i+1)
|
||||||
cubes[j+1].location.x = j-1
|
cubes[j+1].location.x = j-1
|
||||||
cubes[j+1].keyframe_insert(data_path="location", frame=i)
|
cubes[j+1].keyframe_insert(data_path="location", frame=i+1)
|
||||||
|
|
||||||
#add 4 to array counter
|
#add 4 to array counter
|
||||||
arrayCounter.inputs[0].default_value += 4
|
arrayCounter.inputs[0].default_value += 4
|
||||||
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i+1)
|
||||||
|
|
||||||
#rearrange arrays
|
#rearrange arrays
|
||||||
cubes[j], cubes[j + 1] = cubes[j + 1], cubes[j]
|
cubes[j], cubes[j + 1] = cubes[j + 1], cubes[j]
|
||||||
@@ -98,6 +100,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
@@ -141,6 +147,6 @@ def setup_array(count):
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(20)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
bubble_sort(cubes, arrayCounter, comparisonCounter)
|
bubble_sort(cubes, arrayCounter, comparisonCounter)
|
||||||
@@ -13,48 +13,62 @@ def heapify(arr, n, i):
|
|||||||
largest = i # Initialize largest as root
|
largest = i # Initialize largest as root
|
||||||
l = 2 * i + 1 # left = 2*i + 1
|
l = 2 * i + 1 # left = 2*i + 1
|
||||||
r = 2 * i + 2 # right = 2*i + 2
|
r = 2 * i + 2 # right = 2*i + 2
|
||||||
|
|
||||||
# See if left child of root exists and is
|
#add 2 to arrayCounter
|
||||||
# greater than root
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
#add 1 to comparisonCounter
|
||||||
|
comparisonCounter.inputs[0].default_value += 2
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
# See if left child of root exists and is greater than root
|
||||||
if l < n and arr[largest].scale.z - 1 < arr[l].scale.z - 1:
|
if l < n and arr[largest].scale.z - 1 < arr[l].scale.z - 1:
|
||||||
largest = l
|
largest = l
|
||||||
|
|
||||||
# See if right child of root exists and is
|
#add 2 to arrayCounter
|
||||||
# greater than root
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
#add 1 to comparisonCounter
|
||||||
|
comparisonCounter.inputs[0].default_value += 2
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
# See if right child of root exists and is greater than root
|
||||||
if r < n and arr[largest].scale.z - 1 < arr[r].scale.z - 1:
|
if r < n and arr[largest].scale.z - 1 < arr[r].scale.z - 1:
|
||||||
largest = r
|
largest = r
|
||||||
|
|
||||||
# Change root, if needed
|
# Change root, if needed
|
||||||
if largest != i:
|
if largest != i:
|
||||||
arr[i], arr[largest] = arr[largest], arr[i] # swap
|
arr[i], arr[largest] = arr[largest], arr[i] #swap
|
||||||
|
|
||||||
a = arr[i].location.x
|
a = arr[i].location.x
|
||||||
b = arr[largest].location.x
|
b = arr[largest].location.x
|
||||||
|
|
||||||
arr[i].location.x = b
|
arr[i].location.x = b
|
||||||
arr[largest].location.x = a
|
arr[largest].location.x = a
|
||||||
|
|
||||||
|
#add 4 to arrayCounter
|
||||||
|
arrayCounter.inputs[0].default_value += 4
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
for cube in cubes:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
|
||||||
iframe += 1
|
iframe += 1
|
||||||
|
|
||||||
# Heapify the root.
|
# Heapify the root.
|
||||||
heapify(arr, n, largest)
|
heapify(arr, n, largest)
|
||||||
|
|
||||||
# The main function to sort an array of given size
|
# The main function to sort an array of given size
|
||||||
def heap_sort(arr):
|
def heap_sort(arr):
|
||||||
|
|
||||||
n = len(arr)
|
|
||||||
|
|
||||||
global iframe
|
global iframe
|
||||||
|
|
||||||
|
n = len(arr)
|
||||||
|
|
||||||
# Build a maxheap.
|
# Build a maxheap.
|
||||||
for i in range(n//2 - 1, -1, -1):
|
for i in range(n//2 - 1, -1, -1):
|
||||||
heapify(arr, n, i)
|
heapify(arr, n, i)
|
||||||
|
|
||||||
# One by one extract elements
|
# One by one extract elements
|
||||||
for i in range(n-1, 0, -1):
|
for i in range(n-1, 0, -1):
|
||||||
arr[i], arr[0] = arr[0], arr[i] # swap
|
arr[i], arr[0] = arr[0], arr[i] #swap
|
||||||
|
|
||||||
a = arr[i].location.x
|
a = arr[i].location.x
|
||||||
b = arr[0].location.x
|
b = arr[0].location.x
|
||||||
@@ -62,6 +76,10 @@ def heap_sort(arr):
|
|||||||
arr[i].location.x = b
|
arr[i].location.x = b
|
||||||
arr[0].location.x = a
|
arr[0].location.x = a
|
||||||
|
|
||||||
|
#add 4 to arrayCounter
|
||||||
|
arrayCounter.inputs[0].default_value += 4
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
for cube in cubes:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
iframe += 1
|
iframe += 1
|
||||||
@@ -125,6 +143,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
@@ -172,5 +194,5 @@ def setup_array(count):
|
|||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
iframe = 0
|
iframe = 1
|
||||||
heap_sort(cubes)
|
heap_sort(cubes)
|
||||||
@@ -7,9 +7,10 @@ from mathutils import Vector, Matrix
|
|||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def insertion_sort(cubes, arrayCounter, comparisonCounter):
|
def insertion_sort(cubes, arrayCounter, comparisonCounter):
|
||||||
|
|
||||||
#start at frame 0
|
#start at frame 0
|
||||||
iframe=0
|
iframe = 1
|
||||||
originFrame = 0
|
originFrame = 1
|
||||||
|
|
||||||
for i in range(0, len(cubes)):
|
for i in range(0, len(cubes)):
|
||||||
#defines key_item that is compared until correct location
|
#defines key_item that is compared until correct location
|
||||||
@@ -126,6 +127,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
@@ -169,6 +174,6 @@ def setup_array(count):
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(12)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
insertion_sort(cubes, arrayCounter, comparisonCounter)
|
insertion_sort(cubes, arrayCounter, comparisonCounter)
|
||||||
@@ -192,6 +192,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
@@ -235,7 +239,7 @@ def setup_array(count):
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(20)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
iframe = 0
|
iframe = 1
|
||||||
merge_sort(cubes, 0, len(cubes)-1, arrayCounter, comparisonCounter)
|
merge_sort(cubes, 0, len(cubes)-1, arrayCounter, comparisonCounter)
|
||||||
@@ -129,6 +129,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
@@ -172,7 +176,7 @@ def setup_array(count):
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(20)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
iframe = 0
|
iframe = 1
|
||||||
quick_sort(cubes, 0, len(cubes) - 1, arrayCounter, comparisonCounter)
|
quick_sort(cubes, 0, len(cubes) - 1, arrayCounter, comparisonCounter)
|
||||||
@@ -8,7 +8,7 @@ from mathutils import Vector, Matrix
|
|||||||
|
|
||||||
def selection_sort(cubes, arrayCounter, comparisonCounter):
|
def selection_sort(cubes, arrayCounter, comparisonCounter):
|
||||||
|
|
||||||
global iframe
|
iframe = 1
|
||||||
|
|
||||||
for i in range(0, len(cubes)):
|
for i in range(0, len(cubes)):
|
||||||
min_idx = i
|
min_idx = i
|
||||||
@@ -98,6 +98,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
@@ -141,7 +145,6 @@ def setup_array(count):
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(20)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
iframe = 0
|
|
||||||
selection_sort(cubes, arrayCounter, comparisonCounter)
|
selection_sort(cubes, arrayCounter, comparisonCounter)
|
||||||
@@ -116,6 +116,10 @@ def setup_array(count):
|
|||||||
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
|
#add keyframe on frame 0 for comparison and array counter
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=0)
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(0,count-1))
|
ran = list(range(0,count-1))
|
||||||
|
|
||||||
@@ -158,7 +162,7 @@ def setup_array(count):
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setup_array(20)
|
cubes, arrayCounter, comparisonCounter = setup_array(50)
|
||||||
|
|
||||||
iframe = 0
|
iframe = 1
|
||||||
shellSort(cubes, len(cubes), arrayCounter, comparisonCounter)
|
shellSort(cubes, len(cubes), arrayCounter, comparisonCounter)
|
||||||
Reference in New Issue
Block a user