bubble color sort

This commit is contained in:
Xevion
2019-11-08 15:01:02 -06:00
parent 2922a93965
commit ad61bf5684
3 changed files with 67 additions and 1 deletions

View File

@@ -0,0 +1,62 @@
import colorsys, random
def setup():
size(360 * 2, 360 * 2)
global array, carray
frameRate(1000)
array = [x for x in range(max(width, height))]
carray = [getRGB(x, precision=max(width, height)) for x in array]
combined = zip(array, carray)
random.shuffle(combined)
array[:], carray[:] = zip(*combined)
noLoop()
def getRGB(n, precision=360.0):
r, g, b = colorsys.hsv_to_rgb(n / float(precision), 1, 1)
return r * 255, g * 255, b * 255
def getRGBArray(array):
out = [getRGB(x, precision=len(array)) for x in array]
return out
def randHSV():
h = random.random()
r, g, b = colorsys.hsv_to_rgb(h, 1, 1)
return r * 255, g * 255, b * 255
def visualize(array, carray):
for pos, value in enumerate(array):
r, g, b = carray[pos]
stroke(r, g, b)
line((width/2) - value/2, pos, (width/2) + value/2, pos)
def bubbleSort(array, carray):
def swap(x1, x2):
array[x1], array[x2] = array[x2], array[x1]
carray[x1], carray[x2] = carray[x2], carray[x1]
for pos, value in enumerate(array[:-1]):
if value > array[pos + 1]:
swap(pos, pos+1)
return array, carray
def pieChartRender(diameter, array, carray=None):
lastAngle = 0
div = 360.0 / len(array)
for pos, var in enumerate(array):
fill(*carray[pos])
arc(width/2, height/2, diameter, diameter, lastAngle, lastAngle + radians(div))
lastAngle += radians(div)
i = 1
array = []
def draw():
global array, carray
background(255)
pieChartRender(width-10, array, carray)
array, carray = bubbleSort(array, carray)
def mouseClicked():
loop()

View File

@@ -0,0 +1,2 @@
mode=Python
mode.id=jycessing.mode.PythonMode

View File

@@ -8,4 +8,6 @@ Sketches that visualize sorting algorithms as they progress.
- **cocktail_shaker_line** - Sorting of a line using the cocktail shaker algorithm (double bouble sort I believe). Needs reworking as the colors and movement of the line is not as expected (colors shift, movement doesn't properly correlate).
- **insertion_sort_line** - Sorting of a line using the insertion sort algorithm.
- **insertion_sort_line** - Sorting of a line using the insertion sort algorithm.
- **ColorSort** - Spherical rainbow sorted using simple Bubblesort (?) algorithm. Probably the sketch I am most proud of in the sorting category, as the sphere representation was difficult to create ande model.