//生成一个随机数 参数分别为1.生成的个数 2.区间
func getArc4randomArray(count : Int , maxNum :UInt32) -> [Int] {
var arc4randomArr : Array = [Int]();
for _ in 0..<count{
let num : Int = Int(arc4random() % maxNum)+1
arc4randomArr.append(num)
}
return arc4randomArr
}
//快速排序
func quick_SortArr(array : inout [Int]){
if array.count <= 2 {
return
}
quick_SortArr_Center(array: &array, l: 0, r: array.count - 1)
}
func quick_SortArr_Center(array : inout [Int], l : Int ,r : Int){
if l < r {
var positionArr : [Int] = get_SortArray(starIndex: l, endIndex: r, array: &array)
quick_SortArr_Center(array: &array , l: l , r:positionArr[0] - 1)
quick_SortArr_Center(array: &array ,l: positionArr[1] + 1, r:r)
}
}
func get_SortArray(starIndex : Int , endIndex :Int , array :inout [Int]) -> [Int] {
var leftless : Int = starIndex - 1
var current : Int = starIndex
var rightless : Int = endIndex
var num : Int = array[endIndex]
var positionArr = [Int]()
for _ in 0..<(endIndex - starIndex){
var currentNum : Int = array[current]
if currentNum > num {
rightless = rightless - 1;
if(current != rightless){
swap(&array[current], &array[rightless])
}
}else if currentNum < num{
leftless = leftless + 1;
if(current != leftless){
swap(&array[current], &array[leftless])
}
current = current + 1;
}else{
current = current + 1;
}
}
if rightless != endIndex{
swap(&array[rightless], &array[endIndex])
}
positionArr.append(leftless+1);
positionArr.append(rightless);
return positionArr
}
/*
//调用
var arc4random : [Int] = getArc4randomArray(count: 10, maxNum: 10)
quick_SortArr(array: &arc4random)
*/