Skip to content

Instantly share code, notes, and snippets.

@igiu1988
Created October 9, 2013 04:39
Show Gist options
  • Save igiu1988/6896294 to your computer and use it in GitHub Desktop.
Save igiu1988/6896294 to your computer and use it in GitHub Desktop.
def bubbleSort(list):
issorted = False
for i in range(0,len(list)):
issorted = True
# 在这一趟的比较中,如果发现这一趟的数列已经成有序状态,那就不用排了,并且是整个数列也都算排完了
for j in range(0, len(list) - i - 1):
if list[j] > list[j+1]:
temp = list[j]
list[j] = list[j+1]
list[j + 1] = temp
issorted = False;
print "sort ",i, " " , list
if (issorted):
break;
list = [7, 4, 1, 3, 9, 2, 8]
bubbleSort(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment