Created
June 16, 2015 15:12
-
-
Save manxisuo/ce6eadd2ba66286be717 to your computer and use it in GitHub Desktop.
算法学习
This file contains 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
arr = [3, 8, 4, 2, 6, 7, 9, 1, 5] | |
length = len(arr) | |
i = 1 #[1, length - 1] 表示第几趟遍历 | |
j = 0 #[0, length - 1 - i] 表示某一趟遍历中,待比较的两个数中前一个数的位置 | |
while i < length: | |
j = 0 | |
while j < length - i: | |
if arr[j] > arr[j + 1]: | |
tmp = arr[j] | |
arr[j] = arr[j + 1] | |
arr[j + 1] = tmp | |
j += 1 | |
i += 1 | |
print arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment