Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make a sorting algorithm without looping any code in the function?

Can you make like a sorting algorithm something like a quicksort where it'll sorts parts in a model from smallest to tallest but without looping it at all so for example if you were to do

print("Test")

In the start of the sorting it will only print it once?

If it is possible how would you do so?

The only way i know is this


function sortingAlgorithmA(group)
	
	local list = group
	
	local x = 1
	game:GetService("RunService").Heartbeat:Connect(function()
		if x >= #list then return end
		
		x = x + 1
		for i=1, #list do
			if i < #list then
				local current = list[i]
				local adjacent = list[i+1]
				
				if current.Size.Y > adjacent.Size.Y then
					local temp = current.Position
					current.Position = Vector3.new(adjacent.Position.X, current.Position.Y, 0)
					adjacent.Position = Vector3.new(temp.X, adjacent.Position.Y, 0)
					
					current.Ind.Value = not current.Ind.Value
					adjacent.Ind.Value = not adjacent.Ind.Value
					
					temp = list[i]
					list[i] = list[i+1]
					list[i+1] = temp
				end
			end
			end
		
		end)
	
	
end


local group = createSortingGroup(Vector3.new(0,0,0), 15)

wait(1)

randomizeSortingGroup(group)

wait(1)

sortingAlgorithmA(group)

2 comments

0

If you were to do a print it will print it 14 times which i do not wan't it to repeat just sort everything in a single loop

0

There is no way for a program to even compare all the values in a table without at least looping through it once.

2 answers

✓ Accepted answer

There's a built-in quicksort function implemented directly in C that's probably faster than anything anyone can write themselves in Lua.

local t = {3, 1, 2}

table.sort(t, function(a, b)
	return a < b
end)

print(unpack(t)) --> 1, 2, 3
local partsArr = partFolder:GetChildren()

table.sort(partsArr, function(a, b)
    return a.Size.Y > b.Size.Y
end)

-- this will create a table with all the parts and sort them from tallest to smallest.

1 comment

0

Alrighty i'll test it out now

This discussion is preserved as it was. New answers, comments, and votes are closed.