题解 | #排序#

排序

https://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896

-- Lua代码
function Partition(nums, left, right)
    key = nums[left]
 
    while left < right do
        while left < right and key <= nums[right] do
            right = right - 1
        end
        nums[left] = nums[right]
 
        while left < right and key >= nums[left] do
            left = left + 1
        end
        nums[right] = nums[left]
    end
 
    nums[left] = key
    return left
end
 
function QSort(nums, left, right)
    if left >= right then 
        return
    end
 
    keyPos = Partition(nums, left, right)
    QSort(nums, left, keyPos - 1)
    QSort(nums, keyPos + 1, right)
end
 
nums = { 4,2,5,1,3 }
QSort(nums, 1, #nums)
print(table.concat(nums, " "))

https://blog.csdn.net/BingDream/article/details/119875181?spm=1001.2014.3001.5501

全部评论

相关推荐

勇敢的联想人前程似锦:如果我是你,身体素质好我会去参军,然后走士兵计划考研211只需要200多分。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务