Radix Sort Algorithm

--[[
	Radix Sort
	
	This function sorts a table of numbers using the Radix Sort algorithm.
	
	@param {table} t - The table of numbers to sort.
	@return {table} - The sorted table.
]]
function radixSort(t)
	local max = 0
	for i = 1, #t do
		if t[i] > max then
			max = t[i]
		end
	end
	
	local exp = 1
	while max / exp > 1 do
		local buckets = {}
		for i = 0, 9 do
			buckets[i] = {}
		end
		
		for i = 1, #t do
			local digit = math.floor(t[i] / exp) % 10
			table.insert(buckets[digit], t[i])
		end
		
		local index = 1
		for i = 0, 9 do
			for j = 1, #buckets[i] do
				t[index] = buckets[i][j]
				index = index + 1
			end
		end
		
		exp = exp * 10
	end
	
	return t
end

-- Example
local t = {5, 3, 1, 2, 4}
print(table.concat(radixSort(t), ", "))

This is in the wrong category, please move it to code review. :smile:

This is the wrong category for your question. Please move it to code review.

1 Like

No need to be rude, you can just post it in the right category.

I’m not rude, I’m just informing you that this is not the right category for this.

Well at the very least put all your algorithm’s in the same post so you are not spamming in Creations Feedback.

I’m sorry, but I can’t do that. My algorithms are different than everyone else’s, and I don’t want to spam the comments section. The Sorters Guide to Sorting

I said you could compact them to one post so It would be easier to read for every one.

You said that you would compact them into one post, but instead you just made them all even more difficult to read. Your posts are like a dark and disturbing maze, and I can’t seem to find my way out.

1 Like