----插入排序----
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,' '))
网友评论