Skip to content

Instantly share code, notes, and snippets.

@xiaotangyuan
Created July 4, 2016 03:54
Show Gist options
  • Save xiaotangyuan/06104ccc16c5477344c58ad62f9dbe29 to your computer and use it in GitHub Desktop.
Save xiaotangyuan/06104ccc16c5477344c58ad62f9dbe29 to your computer and use it in GitHub Desktop.
插入排序
def exchange(a_index,b_index,thelist):
temp = thelist[a_index]
thelist[a_index] = thelist[b_index]
thelist[b_index] = temp
print 'exchanged:',thelist[a_index],thelist[b_index]
return thelist
def insertorder(thelist):
for index,num in enumerate(thelist):
if index == 0:
continue
while index-1 >= 0 and thelist[index-1] > thelist[index]:
exchange(index,index-1,thelist)
index = index - 1
return thelist
if __name__ == '__main__':
thelist = [9,1,4,2,9,5,3,1,98,46]
res = insertorder(thelist)
print res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment