# selection.py
# Selection Sort Example
from Processing import *
window(500, 500)

# On mousePressed, perform one pass of selection sort
def mousePressed(o, e):
    global start
    selectOnce(items, start)
    if start < len(items)-1:
        start = start + 1

# Perform a complete Selection Sort
def selectionSort(al):
    i = 0
    while i < len(items):
        selectOnce(al, i)
        i += 1

# Perform once pass of Selection Sort.
def selectOnce(al, i):

    # Init to first element
    bestVal = al[i]
    bestIdx = i

    # Start looping at item after current top
    j = i + 1
    while j < len(al):
        # Find best value
        if al[j] < bestVal:
            bestVal = al[j]
            bestIdx = j
        j = j + 1

    # Swap best with top position
    al[bestIdx] = al[i]
    al[i] = bestVal
  
    # Redraw items
    drawList()

# 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
