--快速排序
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
--冒泡排序
local tt ={10,20,1,2,3,5,6,2,3,4}
local tmp = 0
for i=1, #tt - 1 do
for j=1,#tt - i do
if tt[j] > tt[j+1] then
tmp = tt[j]
tt[j] = tt[j+1]
tt[j+1] = tmp
end
end
end
Lua 排序
最新推荐文章于 2024-02-05 13:40:54 发布