首页 > 代码库 > lua 快速排序

lua 快速排序

function quick_sort(list, head, tail)
    if tail > head then
        i = head
        j = tail

        tmp = list[i]        --取第一个元素用于比较 同时腾出第一个位置
        while i < j do
            while j > i do
                if list[j] < tmp then
                    list[i] = list[j]
                    break
                end
                j = j - 1
            end

            while i < j do
                if list[i] > tmp then
                    list[j] = list[i]
                    break
                end
                i = i + 1
            end
        end
        list[i] = tmp

        quick_sort(list, head, i)
        quick_sort(list, i+1, tail)
    end
end