代码语言:javascript复制
//
// main.swift
//
//
// Created by LongMa on 2020/7/4.
// Copyright © 2020 . All rights reserved.
//
import Foundation
print("Hello, World!")
func kp(arr : [Int]) -> Array<Int>{
var a = arr
let len = arr.count
var indexOfMinNum, temp : Int
for i in 0..<(len - 1) {//eg:3个数,应循环2次
indexOfMinNum = i
//无序部分,查找最小值,记录下标。
for j in (i 1)..<(len){//eg:3个数,最大小标为2。
if a[j] < a[indexOfMinNum] {
indexOfMinNum = j
}
}
//交换无序部分0位值和最小值
if i != indexOfMinNum {
temp = a[i]
a[i] = a[indexOfMinNum]
a[indexOfMinNum] = temp
}
}
return a
}
let lArr = [22, 6, 4, 5, 10, 11, 2, 1, 2, 1, 0]
let lA = kp(arr: lArr)
print(lA)
let lArr1 = [23,6,4,8,9,0,1,2,3,2,1]
let lA1 = kp(arr: lArr1)
print(lA1)
log:
代码语言:javascript复制Hello, World!
[0, 1, 1, 2, 2, 4, 5, 6, 10, 11, 22]
[0, 1, 1, 2, 2, 3, 4, 6, 8, 9, 23]
Program ended with exit code: 0