diff --git a/sorting/README.md b/sorting/README.md index 7be07ac..ee2ee88 100644 --- a/sorting/README.md +++ b/sorting/README.md @@ -4,4 +4,6 @@ Sketches that visualize sorting algorithms as they progress. -## Sketches \ No newline at end of file +## Sketches + +- **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). \ No newline at end of file diff --git a/sorting/cocktail_shaker_line/cocktail_shaker_line.pyde b/sorting/cocktail_shaker_line/cocktail_shaker_line.pyde new file mode 100644 index 0000000..77c5d30 --- /dev/null +++ b/sorting/cocktail_shaker_line/cocktail_shaker_line.pyde @@ -0,0 +1,30 @@ +import random, time + +def setup(): + global array + size(1000, 1000) + array = list(range(height)) + random.shuffle(array) + colorMode(HSB, height) + +def draw(): + background(height) + global array + done = True + for index in range(len(array)-1): + if array[index] < array[index + 1]: + array[index], array[index + 1] = array[index + 1], array[index] + done = False + for index in list(range(len(array)-1))[::-1]: + if array[index] < array[index + 1]: + array[index], array[index + 1] = array[index + 1], array[index] + done = False + + for index, num in enumerate(array): + fill(num, height, height) + stroke(num, height, height) + ellipse(num, index, 5, 5) + + if done: + time.sleep(0.5) + random.shuffle(array) diff --git a/sorting/cocktail_shaker_line/sketch.properties b/sorting/cocktail_shaker_line/sketch.properties new file mode 100644 index 0000000..2456b0a --- /dev/null +++ b/sorting/cocktail_shaker_line/sketch.properties @@ -0,0 +1,2 @@ +mode=Python +mode.id=jycessing.mode.PythonMode