selection sort DEMO(linear complexity)

#quote from MIT 'introduction to computation and programming using python, Revised'
def selSort(L):
    """Assumes that L is a list of elements that can be
         compared using >.
       Sorts L in ascending order"""
    suffixStart = 0
    while suffixStart != len(L):
        #look at each element in suffix
        for i in range(suffixStart, len(L)):
            if L[i] < L[suffixStart]:
                #swap position of elements
                L[suffixStart], L[i] = L[i], L[suffixStart]
        suffixStart += 1


L = [1, 10, 2, 9, 7, 100, 99, 23, 0]


selSort(L)


print L
[0, 1, 2, 7, 9, 10, 23, 99, 100]

你可能感兴趣的:(python)