Animated sorting algorithms

A simple animated visualization of sorting algorithms:


--# Main
-- Sorter

function setup()
    num = 50
    shuffle()
    parameter.integer("num", 10, 200, num)
    parameter.action("new", shuffle)
    --parameter.action("reverse", reverse)
    parameter.action("selection sort", function()
        sort(selection_sort)
    end)
    parameter.action("bubble sort", function()
        sort(bubble_sort)
    end)
    parameter.action("quicksort", function()
        sort(quick_sort)
    end)
end

function shuffle()
    maxv = HEIGHT-10
    data = {}
    for i=1,num do
        data[#data + 1] = math.random(1, maxv)
    end
end

function reverse()
    for i=1,num/2 do
        data[i],data[num+1-i] = data[num+1-i],data[i]
    end
end

function sort(f)
    output.clear()
    print("Sorting ...")
    opc = 0  -- operations count
    sorter = coroutine.create(f)
    coroutine.resume(sorter)
end

function step()
    opc = opc + 1
    if opc % math.floor(num/10) == 0 then
        coroutine.yield()
    end
end

function selection_sort()
    for p=1,num-1 do
        local m,mj = data[p],p
        for i=p+1,num do
            if data[i] < m then
                m = data[i]
                mj = i
            end
            step()
        end
        data[mj],data[p] = data[p],m
    end
end

function bubble_sort()
    for i=1,num-1 do
        for j=1,num-i do
            local x,y = data[j],data[j+1]
            if x > y then
                data[j+1],data[j] = x,y
            end
            step()
        end
    end
end

function quick_sort()
    function quicksort(a, l, r)
        if l < r then
            local j = partition(a, l, r)
            quicksort(a, l, j-1)
            quicksort(a, j+1, r)
        end
    end
    function partition(a, l, r)
        local pivot = a[l]
        local i, j = l, r+1
        while true do
            repeat i = i + 1; step() until (i > r or a[i] > pivot)
            repeat j = j - 1; step() until (a[j] <= pivot)
            if i >= j then break end
            a[i],a[j] = a[j],a[i]
            step()
        end
        a[l],a[j] = a[j],a[l]
        step()
        return j
    end
    quicksort(data, 1, num)
end

-- This function gets called once every frame
function draw()
    background(40, 40, 50)
    strokeWidth(0)
    noSmooth()

    -- Draw colored bars representing numbers
    local w = (WIDTH - 10)/num
    for i=1,num do
        local v = data[i]
        if v then
            fill(255*(1-v/maxv),255*(1-math.abs(v/maxv*2-1)),255*v/maxv)
            rect(5 + (i-1)*w, 5, w-1, v)       
        end
    end
    
    -- animate sort
    if sorter then
        ok,err = coroutine.resume(sorter)
        if not ok and coroutine.status(sorter) == "dead" then
            print("Done. Count = " .. opc)
            sound(SOUND_PICKUP, 37659)
            sorter = nil
        end
    end    
end

@Juce - you might be interested in these

http://codea.io/talk/discussion/4852/bubble-sort/p1

http://codea.io/talk/discussion/2003/bubble-sort-animation-for-education/p1

http://codea.io/talk/discussion/3758/bubble-sort-quick-sort-and-swap-sort/p1

@Ignatz, thanks, i’ll take a look.
(My son was interested in various sorting algorithms, so i made this as a quick toy demo for him. It was fun to visualize/animate them to see the differences)

yes, animating it makes it much easier to see what is happening

I updated the first post to include a small fix to the stepping part - makes the animation slower and more clear. Also, here is the same code via Github gist:

https://gist.github.com/anonymous/46dd319569c0398c859c

nice @juce