Binary Insertion Sort

Binary Insertion Sort for review.rbxl (94.2 KB)

This is a part of a module making a bunch of different sorting algorythms for practice. My algorithm for Binary Insertion Sort is unsatisfactory since I was unable to fully grasp how to do bitonic search. I was able to use bitonic sort (the middle while section) to find the index the number is adjacent to, but not if it was greater or smaller than that. Which is why I have the FinalValue if block at the end.

module.BinaryInsertionSort = function()
	t = os.clock()
	local List = InterPretVisualization()

	for PointerA = 2, #List do
		Read(List[PointerA])
		local Height = tonumber(List[PointerA].Name)
		
		local Top = PointerA - 1
		local Bottom = 1
		local CorrectPlace
		while Top ~= Bottom and Top > Bottom do
			local Middle = math.ceil((Bottom+Top)/2)
			local Value = tonumber(List[Middle].Name)
			Read(List[Middle])
			
			if Value > Height then
				Top = Middle - 1
			else
				Bottom = Middle + 1
			end
		end

		local FinalValue = tonumber(List[Top].Name) -- This can use top or bottom because they are the same
		if Height > FinalValue then--I want to remove this and do the correct
			CorrectPlace = Top+1
		else
			CorrectPlace = Top
		end
		
		for i = PointerA-1, CorrectPlace, -1 do
			Swap(List,i+1,i)
		end
	end
	
	Complete()
end

I would really like for some way to figure out if the index is greater or smaller inside of the main bitonic search. I was unable to do so, so I am asking for help.

Read() and Complete() are only for visualization.

In the game, you can run local S = require(game.ServerScriptService.Sorts:Clone()) S.BinaryInsertionSort() to sort the bars, or local S = require(game.ServerScriptService.Sorts:Clone()) S.Generator.GeneratePerfect(2^11,0.1) to generate a completely new random array.