Last active
April 5, 2017 22:47
-
-
Save creaaa/11ca83339209b264d542074a1175c22c to your computer and use it in GitHub Desktop.
swiftでselectionSort
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
func selectionSort(array: [Int]) { | |
var array = array | |
var i = 0 | |
// 要素数=7なら、6回走査すれば完了だよ。 | |
while i < array.count-1 { | |
var minIdx = i // 最初は0 | |
for idx in i+1...array.count-1 { // 最初は1 | |
if array[idx] < array[minIdx] { | |
minIdx = idx | |
} | |
} | |
if i != minIdx { | |
swap(&array[i], &array[minIdx]) | |
} | |
print("\(i+1)週目完了! : \(array)") | |
i += 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment