# bubble.py
# Bubblesort Example
from Processing import *
window(500, 500)

# On mousePressed, bubble once
def mousePressed(o, e):
    bubbleOnce(items)

# Perform a complete Bubblesort
def bubbleSort(al):
    while True:
        if bubbleOnce(al) == False:
            break

# Perform one pass of Bubblesort.
def bubbleOnce(al):
    changed = False

    # Loop over all pairs
    i = 0
    while i < len(al)-1:
        s1 = items[i]
        s2 = items[i+1]
    
        # Swap if pair is not in order
        if s1 > s2:
          items[i] = s2
          items[i+1] = s1
          changed = True

        i += 1

    # Redraw list if changed
    drawList()

    # Return True if list changed
    return changed

# Draw the ArrayList to the sketch
def drawList():
    background(0)
    fill(255)
    textSize(20)

    y=100
    for a in items:
        text(a, 100, y)
        y = y + 50
    redraw()

# Fill a list
items = []
items.append("Purin")
items.append("Landry")
items.append("Chococat")
items.append("Pekkle")
items.append("Cinnamoroll")

# Track start of unsorted portion of list
start = 0

# Draw once to get started
drawList()

# Perform one sort step on mouse pressed
onMousePressed += mousePressed

