用Swift做的一个快速排序算法(递归法)
func QuickSort(index: NSMutableArray,left: Int,right: Int) {
if left >= right {
return
}
var i = left
var j = right
let key = index[left] as! Int
while(i<j) {
while(i < j && key >= index[j] as! Int) {
j = j - 1
}
index.replaceObjectAtIndex(i,withObject: index[j])
while(i < j && key <= index[i] as! Int) {
i = i + 1
}
index.replaceObjectAtIndex(j,withObject: index[i])
}
index.replaceObjectAtIndex(i,withObject: key)
QuickSort(index,left: left,right: i - 1)
QuickSort(index,left: i + 1,right: right)
}