题目:输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4.
核心代码:
`
func leastMinNumbers(arr:[Int],k:Int) -> [Int] {
var data:[Int] = arr
var result:[Int] = []
quickSort(arr: &data, low: 0, high: data.count-1)
for i in 0.. high {
return
}
let middle = partition(arr: &arr, low: low, high: high)
quickSort(arr: &arr, low: low, high: middle-1)
quickSort(arr: &arr, low: middle+1, high: high)
}
func partition(arr:inout [Int],low:Int,high:Int) -> Int {
let root:Int = arr[high]
var index:Int = low
for i in low..
测试代码:
`
var searchMin:SearchMin = SearchMin()
var data:[Int] = [4,5,1,6,2,7,3,8]
var k:Int = 4
var result:[Int] = searchMin.leastMinNumbers(arr: data, k: k)
print("FlyElephant---最小的(k)个数---(result)")`