A python example based blog that shows how to accomplish python goals and how to correct python errors.
Monday, December 16, 2013
Python - bubble sort or was that sift sort
# Here is an implementation of bubble sort
# or as some call it sift sort, settle sort,
# whatever you want to call it, here it is:
def siftSort(originalList):
size = len(originalList)
sifted = False
while not sifted:
sifted = True
for i in range(0, len(originalList)):
pos = i
if (i>=size-1):
break;
if originalList[pos] > originalList[pos+1]:
sifted = False;
originalList[pos+1], originalList[pos] = originalList[pos], originalList[pos+1]
return originalList
if (__name__ == '__main__'):
# my 'random' list that needs sorting
myList = [987,9999,25,61,0,14,61,1234,99,986]
print("the original list: {0}".format(myList))
print("the sorted list: {0}".format(siftSort(myList)))
# output:
# the original list: [987, 9999, 25, 61, 0, 14, 61, 1234, 99, 986]
# the sorted list: [0, 14, 25, 61, 61, 99, 986, 987, 1234, 9999]
Subscribe to:
Posts (Atom)