Lua快排

--[[
文件名	: 	C++入口.lua
作者  	:  	陈泽丹
创建时间:  	2013-11-1
描述	:
--]]


--得分排名
function SortScore( _sort_list )
	local function IsNeedFront( _l_item, _r_item )
		if _l_item.TOTAL_RIGH < _r_item.TOTAL_RIGH then
			return true
		end
		if _l_item.TOTAL_RIGH == _r_item.TOTAL_RIGH then
			if _l_item.USE_TM >= _r_item.USE_TM then
				return true
			end
			return false
		end
		return false
	end
	local function DoSortScore( _t_item, _l, _r )
		if _l < _r then
			local mid_val = _t_item[_l]
			local i = _l
			local j = _r
			while i < j do
				--print( i, g_sort_list[i].TOTAL_RIGH, g_sort_list[i].USE_TM )
				--print( j, g_sort_list[j].TOTAL_RIGH, g_sort_list[j].USE_TM )
				while i < j and IsNeedFront( _t_item[j], mid_val ) do
					j = j - 1
				end
				if i < j then
					_t_item[i] = _t_item[j]
				end

				while i < j and IsNeedFront( mid_val, _t_item[i] ) do
					i = i + 1
				end
				if i < j then
					_t_item[j] = _t_item[i]
				end
			end
			_t_item[i] = mid_val
			DoSortScore( _t_item, _l, i-1 )
			DoSortScore( _t_item, i+1, _r )
		end
	end
	DoSortScore( _sort_list, 1, table.getn(_sort_list) )
end

G_LIMIT = 1000
g_sort_list = {}
for i=1, G_LIMIT do
	g_sort_list[i] = {}
	g_sort_list[i].TOTAL_RIGH = math.random( 1, 30 )
	g_sort_list[i].USE_TM = math.random( 1, 10 )
	--print( g_sort_list[i].TOTAL_RIGH, g_sort_list[i].USE_TM )
end
print( "--------------" )
SortScore( g_sort_list )
print( "--------------" )
for i=1, G_LIMIT do
	print( i, g_sort_list[i].TOTAL_RIGH, g_sort_list[i].USE_TM )
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值