Sorting an Edge List

Anthony D'Agostino gamma-ray at dope.comcast.net
Fri Apr 29 23:37:39 EDT 2005


I found my old bubble sort solution:

============================================
def esort(edges):
    while 1:
        swaps = 0
        for j in range(len(edges)-2):
            if edges[j][1] != edges[j+1][0]:
                edges[j+1],edges[j+2] = edges[j+2],edges[j+1] # swap
                swaps = 1
        if swaps == 0: break
    return edges

print esort([('A','Y'), ('J','A'), ('Y','J')])
print esort([(5,0), (6, -12), (0,6), (-12, 3)])
============================================

The list can be any length and there will always be multiple valid 
solutions, depending on which edge you start with. I'm using this to sort 
edges for mesh subdivision. I just thought there might be a more elegant way 
to write it. 





More information about the Python-list mailing list