lua实现switch和快速排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_31803737/article/details/84847698

lua实现switch和快速排序

快速排序

    local list = {.....}
    local comparefuc = function (a, b)
		return a > b	
	 end
    				     
    local function quick_sort(left, right)
        if left >= right then
            return
        end
        
        local i = left
        local j = right
        local key  = list [left]
        
        while (i < j) do
            while i < j and comparefuc(list[j], key) do
                j = j - 1
            end
            list[i] =  list [j]
            while i < j and comparefuc(key, list[i]) do
                i = i + 1
            end
            list[j] = list[i]
        end
        list[i] = key
        
        quick_sort(left, i - 1)
        quick_sort(i + 1, right)
    end
    
    quick_sort(1, #list)

switch

local switch = {
	[case1] = fuction (..)
		return value
	end,
	[case2] =  fuction (..)
		return value
	end,
	[case3] =  fuction (..)
		return value
	end,
	....
}

local case = ...
local value= switch[case]

猜你喜欢

转载自blog.csdn.net/sinat_31803737/article/details/84847698