Tuesday, July 9, 2019

Bubble sort on a list of numbers

Script

#!/usr/local/bin/python3.4

list = [148, 190, 200, 162, 180, 144, 217, 222, 233, 270, 160, 177, 191, 146]

print ("Initial list:  ", list)
print ("")
print ("")


for i in range(0, len(list) - 1):
        for j in range(i + 1, len(list)):
                if(list[i] > list[j]):
                        temp = list[i]
                        list[i] = list[j]
                        list[j] = temp

print("Sorted list: ", list)
print ("")
print ("")


Execution

Initial list:   [148, 190, 200, 162, 180, 144, 217, 222, 233, 270, 160, 177, 191, 146]


Sorted list:  [144, 146, 148, 160, 162, 177, 180, 190, 191, 200, 217, 222, 233, 270]

No comments:

Post a Comment