sortvis.org


sorting algorithm visualisation

cyclesort

I cover the mechanics of cyclesort in great detail in this blogpost.

code

def cyclesort(lst):
    for i in range(len(lst)):
        if i != lst[i]:
            n = i
            while 1: 
                tmp = lst[int(n)]
                if n != i:
                    lst[int(n)] = last_value
                    lst.log()
                else:
                    lst[int(n)] = None
                    lst.log()
                last_value = tmp
                n = last_value
                if n == i:
                    lst[int(n)] = last_value
                    lst.log()
                    break

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

Copyright 2010 Aldo Cortesi