Created
December 3, 2018 17:42
-
-
Save zhuifengshen/d94d9fc3e3e6fccb187a47a2b5a0bdbd 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
# 实现一 | |
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