快速排序

func sortArray(nums []int) []int {
   return qsort(nums,0,len(nums)-1)
}

func qsort(nums []int,left int,right int) []int{
    if left < right{
        mid :=  partition(nums,left,right)
        qsort(nums,left,mid-1)
        qsort(nums,mid+1,right)
    }
    return nums
}


func partition(arr []int,left int,right int) int{
    l := left
    r := right
    tmp := arr[left]
    for l < r{
        for l < r && arr[r] >= tmp {
            r--
        }
        arr[l] = arr[r]
        for l< r && arr[l]<=tmp{
            l++
        }
        arr[r] = arr[l]
    }
    arr[l] = tmp
    return l
}

你可能感兴趣的:(快速排序)