Created
July 4, 2016 03:54
-
-
Save xiaotangyuan/06104ccc16c5477344c58ad62f9dbe29 to your computer and use it in GitHub Desktop.
插入排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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