Skip to content

Instantly share code, notes, and snippets.

@zhuifengshen
Created December 3, 2018 17:42
Show Gist options
  • Save zhuifengshen/d94d9fc3e3e6fccb187a47a2b5a0bdbd to your computer and use it in GitHub Desktop.
Save zhuifengshen/d94d9fc3e3e6fccb187a47a2b5a0bdbd to your computer and use it in GitHub Desktop.
选择排序算法
# 实现一
def selectionSort(arr):
for i in range(len(arr) - 1):
tmp = i
for j in range(i+1, len(arr)):
if arr[tmp] > arr[j]:
tmp = j
if i != tmp:
arr[i], arr[tmp] = arr[tmp], arr[i]
# 实现二
def findSmallest(arr):
smallest = arr[0]
index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
index = i
return index
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
newArr.append(arr.pop(smallest))
return newArr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment