Skip to content

Instantly share code, notes, and snippets.

@manxisuo
Created June 16, 2015 15:12
Show Gist options
  • Save manxisuo/ce6eadd2ba66286be717 to your computer and use it in GitHub Desktop.
Save manxisuo/ce6eadd2ba66286be717 to your computer and use it in GitHub Desktop.
算法学习
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