QuickSort 标准快速排序算法

QuickSort 标准快速排序算法

import Foundation


var sortArr :[Int]  = [1,2,3,5,8,88,65,37,21,9,11,54,32,86,43,55,90,27,6,80]
print("排序前------\(sortArr)");

func quickSort ( arr:inout [Int] ,left:Int,right:Int) ->Void{
    if left >= right {
        return
    }
    var low  = left
    var high = right
    let key = arr[left]
    while low < high {
        while low < high && arr[high] > key {//比key小的左移
            high -= 1;
        }
        arr[low] = arr[high];
        while low < high && arr[low] < key {//比key大的右移
            low += 1;
        }
        arr[high] = arr[low];
    }
    arr[low] = key;//key的位置
    //接着递归处理即可
    quickSort(arr: &arr, left: left, right: low-1);
    quickSort(arr: &arr, left: low+1, right: right);
}

quickSort(arr: &sortArr,left: 0,right: sortArr.count-1);

print("排序后------\(sortArr)")

你可能感兴趣的:(IOS)