sortvis.org


sorting algorithm visualisation

cocktailsort

+
-
1:1
[ ]
drag to pan, scroll to zoom, view raw

code

def cocktailsort(lst):
    begin, end = 0, len(lst) - 1
    finished = False
    while not finished:
        finished = True
        for i in xrange(begin, end):
            if lst[i] > lst[i + 1]:
                lst[i], lst[i + 1] = lst[i + 1], lst[i]
                lst.log()
                finished = False
        if finished:
            break
        finished = True
        end -= 1
        for i in reversed(xrange(begin, end)):
            if lst[i] > lst[i + 1]:
                lst[i], lst[i + 1] = lst[i + 1], lst[i]
                lst.log()
                finished = False
        begin += 1

List order is sampled for visualisation whenever lst.log() is called.

Copyright 2010 Aldo Cortesi