6种排序的lua实现

来源:互联网 发布:在线下单系统源码 编辑:程序博客网 时间:2024/06/06 20:38
require"math"
-- straight insertion sort
a = {3,1,8,4,5,2,7,9,6}
local b = function()
    local string
    for i = 1, 9 do
        print(a[i])
    end
end
b()
local length = table.getn(a)
print("a is length : ",length)
local c = function()
    for i = 2, length do
        if a[i] < a[i-1] then
            local temp = a[i]
            local j = i
            while j > 1 and (a[j-1] > temp)  do
                a[j] = a[j-1]
                j = j - 1
            end
            a[j] = temp
        end
    end
end
print("straight insertion sort")
c()
b()

a = {3,1,8,4,5,2,7,9,6}
local d = function()
    for i = 1,length - 1 do
        for j = 1, length-i do
            if a[j] > a[j+1] then
                a[j], a[j+1] = a[j+1],a[j]
            end
        end
    end
end
print("reset")
b()
print("Bubble sort")
d()
b()
a = {3,1,8,4,5,2,7,9,6}
local e = function()
    for i = 1, length-1 do
        local minlag = i
        for j = i+1, length do
            if a[j] < a[minlag] then
                minlag = j
            end
        end
        if minlag > i then
            local temp
            temp = a[i]
            a[i] = a[minlag]
            a[minlag] = temp
        end
    end
end
local e2 = function()
    for i = 1, (length+1)/2 do
        local minlag = i
        local maxlag = i
        for j = i+1, length-i+1 do
            if a[j] < a[minlag] then
                minlag = j
            end
            if a[j] > a[maxlag] then
                maxlag = j
            end
        end
        if minlag > i then
            local temp
            temp = a[i]
            a[i] = a[minlag]
            a[minlag] = temp
        end
        if maxlag > i then
            local temp
            temp = a[length-i+1]
            a[length-i+1] = a[maxlag]
            a[maxlag] = temp
        end
    end
end
print("reset")
a = {3,1,8,4,5,2,7,9,6}
b()
print("simple selection sort")
e()
b()
print("reset")
a = {3,1,8,4,5,2,7,9,6}
b()
print("simple selection sort TWO")
e2()
b()

print("reset")
a = {3,1,8,4,5,2,7,9,6}
b()
print("Quick Sort")
-- Quick sort 比我小的站前面,比我大的站后面
-- 递归的想法,把问题复杂问题简单的抽象成一个
--function partition is simple pattern
local partition = function(as,low,high)
    local _z = as[low]
    while low < high do
        while low < high and as[high] > _z do
            
            high = high - 1
        end
        as[low], as[high] = as[high], as[low]
        while low < high and as[low] < _z do
            low = low + 1
        end
        as[low], as[high] = as[high], as[low]
        return low
    end
end

QuickSort = function(as, low, high)
    if low < high then
        local zhong = partition(as, low, high)
        print("zhong", zhong)
        print("low, left", low, zhong - 1 )
        QuickSort(as, low, zhong - 1) -- 1 2
        print("right, high", zhong + 1,high)
        QuickSort(as, zhong + 1, high) -- 4 9
    end
end
QuickSort(a,1,9)
b()
print("reset")
a = {3,1,8,4,5,2,7,9,6}
b()
local shellsort = function(a, length)
    local zl = math.floor(length/2)
    while zl > 0  do
        -- print("zl",zl)
        for j = zl+1,zl*2,1 do
            -- print("j",j)
            for i = j,length,zl do
                -- print("i",i)
                if a[i] < a[i-zl] then
                    local temp = a[i]
                    while (i-zl) > 0 and temp < a[i-zl] do
                        a[i] = a[i-zl]
                        i = i - zl
                    end
                    a[i] = temp
                end
            end
        end
        zl = math.floor(zl/2)
    end
end
print("Shell sort")
shellsort(a,9)
b()
print("reset")
a = {3,1,8,4,5,2,7,9,6}
b()

-- 调整heap 简单原型
heapadjust = function(a,i,size)
    print("math.ceil(size/2)", math.ceil(size/2))
    if i <= math.floor(size/2) then
        local max = i
        local lchild,rchild = 2*i, 2*i+1
        print("lchild,rchild", lchild, rchild)
        if lchild <= size and a[lchild] > a[max] then
            max = lchild
        end
        if rchild <= size and a[rchild] > a[max] then
            max = rchild
        end
        print("max", max)
        if max ~= i then
            a[max],a[i] = a[i],a[max]
            heapadjust(a,max,size)
        end
    else
        print("递归结束")
    end
end

local buildheap = function(a,size)
    for i = math.floor(size/2),1,-1 do
        print("i", i)
        heapadjust(a,i,size)
    end
end

local HeapSort = function(a,size)
    print("buildheap action---------")
    buildheap(a,size)
    print("buildheap end-----------")
    for i = size, 1, -1 do
        a[1], a[i] = a[i], a[1]
        heapadjust(a, 1, i-1)
    end
end
print("HeapSort")
HeapSort(a,9)
b()

b = {1,3,5,6}
local binarySearch = function(a,value) --默认index is 1 begin  找到对应value值在table 中的下标
    if not a or #a ==0 then
        return nil
    end
    local low, high = 1, #a
    while low <= high do
        print("low, high",low, high)
        local m = math.floor((low+high)/2)
        if value > a[m] then
            low = m + 1
        elseif value < a[m] then
            high = m - 1
        else
            return m
        end
    end
end
local index = binarySearch(b,6)
print("index is :",index)
原创粉丝点击