其他分享
首页 > 其他分享> > 快速排序(lua实现)

快速排序(lua实现)

作者:互联网

---快速排序---
local function QuickSort(t, lowIndex, hightIndex)
    if lowIndex >= hightIndex then
        return
    end

    local low = lowIndex
    local hight = hightIndex
    local base = t[low]
    
    while low < hight do
        --从右往左找小于base的数
        while t[hight] >= base and low < hight do
            hight = hight - 1
        end
        t[low] = t[hight]

        --从左往右找大于base的数
        while t[low] < base and low < hight do
            low = low + 1
        end
        t[hight] = t[low ]
    end
    t[low] = base
    QuickSort(t, lowIndex, low)
    QuickSort(t, low+1, hightIndex)
end
-----QuickSort test--------
local t = {5,1,3,6,3,4,2,3}
print("---before QuickSort sort---"..table.concat(t,' '))
QuickSort(t, 1, 8)
print("---after QuickSort sort---"..table.concat(t,' '))

标签:end,QuickSort,hight,---,lua,base,low,排序,快速
来源: https://blog.csdn.net/hj3601947/article/details/119531309