Update QuickSort.py
This commit is contained in:
70
QuickSort.py
70
QuickSort.py
@@ -57,7 +57,7 @@ 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])
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(1,count+1))
|
ran = list(range(0,count))
|
||||||
|
|
||||||
#randomize array order
|
#randomize array order
|
||||||
random.shuffle(ran)
|
random.shuffle(ran)
|
||||||
@@ -85,58 +85,80 @@ i = 0
|
|||||||
for ob in bpy.data.objects:
|
for ob in bpy.data.objects:
|
||||||
if ob.type == 'MESH' and ob.name != "Counter":
|
if ob.type == 'MESH' and ob.name != "Counter":
|
||||||
origin_to_bottom(ob)
|
origin_to_bottom(ob)
|
||||||
ob.scale.z = ran[i]
|
ob.scale.z = ran[i]+1
|
||||||
cubes.append(ob)
|
cubes.append(ob)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
#sort array based on location.x
|
#sort array based on location.x
|
||||||
cubes.sort(key = lambda obj: obj.location.x)
|
cubes.sort(key = lambda obj: obj.location.x)
|
||||||
|
|
||||||
# Quick sort in Python
|
iframe=0
|
||||||
|
|
||||||
# function to find the partition position
|
# function to find the partition position
|
||||||
def partition(array, low, high):
|
def partition(array, low, high):
|
||||||
|
|
||||||
|
global iframe
|
||||||
|
|
||||||
|
#add 1 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 1
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
# choose the rightmost element as pivot
|
# choose the rightmost element as pivot
|
||||||
pivot = array[high]
|
pivot = array[(high + low) // 2]
|
||||||
|
|
||||||
# pointer for greater element
|
# pointer for greater element
|
||||||
i = low - 1
|
i = low
|
||||||
|
j = high
|
||||||
|
while True:
|
||||||
|
|
||||||
# traverse through all elements
|
iframe += 1
|
||||||
# compare each element with pivot
|
|
||||||
for j in range(low, high):
|
|
||||||
if array[j].scale.z <= pivot.scale.z:
|
|
||||||
# if element smaller than pivot is found
|
|
||||||
# swap it with the greater element pointed by i
|
|
||||||
i = i + 1
|
|
||||||
array[i].location.x = j
|
|
||||||
array[j].location.x = i
|
|
||||||
|
|
||||||
# swapping element at i with element at j
|
for cube in cubes:
|
||||||
(array[i], array[j]) = (array[j], array[i])
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
|
||||||
|
while array[i].scale.z < pivot.scale.z:
|
||||||
|
|
||||||
# swap the pivot element with the greater element specified by i
|
#add 1 to comparison counter
|
||||||
array[i + 1].location.x = high
|
comparisonCounter.inputs[0].default_value += 1
|
||||||
array[high].location.x = i + 1
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
(array[i + 1], array[high]) = (array[high], array[i + 1])
|
i += 1
|
||||||
|
|
||||||
# return the position from where partition is done
|
while array[j].scale.z > pivot.scale.z:
|
||||||
return i + 1
|
|
||||||
|
#add 1 to comparison counter
|
||||||
|
comparisonCounter.inputs[0].default_value += 1
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
if i >= j:
|
||||||
|
return j
|
||||||
|
|
||||||
|
array[i].location.x = j
|
||||||
|
array[j].location.x = i
|
||||||
|
|
||||||
|
array[i].keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
array[j].keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
|
||||||
|
#add 4 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 4
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
# swapping element at i with element at j
|
||||||
|
array[i], array[j] = array[j], array[i]
|
||||||
|
|
||||||
# function to perform quicksort
|
# function to perform quicksort
|
||||||
def quickSort(array, low, high):
|
def quickSort(array, low, high):
|
||||||
if low < high:
|
if low < high:
|
||||||
|
|
||||||
# find pivot element such that
|
# find pivot element such that
|
||||||
# element smaller than pivot are on the left
|
# element smaller than pivot are on the left
|
||||||
# element greater than pivot are on the right
|
# element greater than pivot are on the right
|
||||||
pi = partition(array, low, high)
|
pi = partition(array, low, high)
|
||||||
|
|
||||||
# recursive call on the left of pivot
|
# recursive call on the left of pivot
|
||||||
quickSort(array, low, pi - 1)
|
quickSort(array, low, pi)
|
||||||
|
|
||||||
# recursive call on the right of pivot
|
# recursive call on the right of pivot
|
||||||
quickSort(array, pi + 1, high)
|
quickSort(array, pi + 1, high)
|
||||||
|
|
||||||
|
|
||||||
|
quickSort(cubes, 0, len(cubes) - 1)
|
||||||
Reference in New Issue
Block a user