Wednesday, June 10, 2015

Sorting a list with out sort/sorted functions - Python

lst = [1,0,4,5,6,7,8,0,0,0]

print(lst)
cou = len(lst)
for i in range(cou):
    for j in range(1, cou-i):
       #descending
        """if lst[j] > lst[j-1]:
            (lst[j-1], lst[j]) = (lst[j], lst[j-1])"""
        #ascending  
        if lst[j] < lst[j-1]:
            (lst[j-1], lst[j]) = (lst[j], lst[j-1])
print(lst)

No comments:

Post a Comment