二、算法——05、插入排序(lua实现)

2020-10-21  本文已影响0人  GameObjectLgy
----插入排序----
local function InsertSort(t)
    local i
    for i = 2, #t do
        local j = i - 1
        local temp = t[i]
        while j >= 1 and t[j] >= temp do
            t[j+1] = t[j]
            j = j - 1
        end
        t[j+1] = temp--给最后一位赋值
    end
end

--InsertSort test---
local t = {5,1,3,6,3,4,2,3}
print("---before insert sort---")
print(table.concat(t,' '))
print("---after insert sort---")
InsertSort(t)
print(table.concat(t,' '))
上一篇 下一篇

猜你喜欢

热点阅读