- Joined
- Dec 13, 2018
- Messages
- 2,368
Hello Hive
I translated Merge Sort [GUI Friendly] v1.01 to Lua using cJass2Lua(v1.09) - Powerfull cJass and JASS converter and the map compiles fine without errors, and the merge sort is being ran. The problem is that I am getting unexpected results, and the units aren't properly sorted.
Furthermore one of the variables in one of the loops had to be changed for the map to even compile.
Doesn't compile;
Compiles;
Does anyone know what is going on?
Here's the translated code;
I translated Merge Sort [GUI Friendly] v1.01 to Lua using cJass2Lua(v1.09) - Powerfull cJass and JASS converter and the map compiles fine without errors, and the merge sort is being ran. The problem is that I am getting unexpected results, and the units aren't properly sorted.
Furthermore one of the variables in one of the loops had to be changed for the map to even compile.
Doesn't compile;
-
For each (Integer BattleSystem_Int3) from 1 to TotalCurrentUnits , do (Actions)
-
Loop - Actions
-
Set MSort_Values[BattleSystem_Int3] = (Real(BattleSystem_TurnSpeed[BattleSystem_Int3]))
-
Set BattleSystem_Int4 = (BattleSystem_Int4 + 1)
-
Set MSort_IndexInitial[BattleSystem_Int3] = BattleSystem_Int4
-
-
Compiles;
-
For each (Integer BattleSystem_Int3) from 1 to 7, do (Actions)
-
Loop - Actions
-
Set MSort_Values[BattleSystem_Int3] = (Real(BattleSystem_TurnSpeed[BattleSystem_Int3]))
-
Set BattleSystem_Int4 = (BattleSystem_Int4 + 1)
-
Set MSort_IndexInitial[BattleSystem_Int3] = BattleSystem_Int4
-
-
Does anyone know what is going on?
Here's the translated code;
Lua:
-- TESH.scrollpos=89
-- TESH.alwaysfold=0
-- Merge Sort [GUI Friendly] v1.01
-- by Flux
-- Flux
--
-- Merge Sort is an efficient sorting algorithm
-- that has a worst case performance of O(n logn)
-- This system allows you to sort values of an array
-- in either ascending or descending order
-- Additionally, it this system allows you to get the
-- sorted indices of the array values.
--
-- Purely written in JASS and doesn't require anything
---[USER=121133]@Return[/USER] nothing
function MSort_Display()
local i = udg_MSort_StartIndex
BJDebugMsg("========== |cffffcc00[Merge Sort]:|r Data ===============")
while true do
if i > udg_MSort_EndIndex then break end
BJDebugMsg("MSort_Values[" .. I2S(i) .. "] = " .. R2S(udg_MSort_Values) .. ", MSort_Indices[" .. I2S(i) .. "] = " .. I2S(udg_MSort_Indices))
i = i + 1
end
BJDebugMsg("============== End Display =================\n")
end
---@param leftStart integer
---@param leftEnd integer
---@param rightStart integer
---@param rightEnd integer
---[USER=121133]@Return[/USER] nothing
function MSort_Merge(leftStart, leftEnd, rightStart, rightEnd)
local i = leftStart
local j = rightStart
local k = i
print("Hello World")
while true do
if i > leftEnd or j > rightEnd then break end
if udg_MSort_Ascending then
if udg_MSort_Left < udg_MSort_Right[j] then
udg_MSort_Values[k] = udg_MSort_Left
udg_MSort_Indices[k] = udg_MSort_IndexInitial
k = k + 1
i = i + 1
else
udg_MSort_Values[k] = udg_MSort_Right[j]
udg_MSort_Indices[k] = udg_MSort_IndexInitial[j]
k = k + 1
j = j + 1
end
else
if udg_MSort_Left > udg_MSort_Right[j] then
udg_MSort_Values[k] = udg_MSort_Left
udg_MSort_Indices[k] = udg_MSort_IndexInitial
k = k + 1
i = i + 1
else
udg_MSort_Values[k] = udg_MSort_Right[j]
udg_MSort_Indices[k] = udg_MSort_IndexInitial[j]
k = k + 1
j = j + 1
end
end
end
-- Fill up remaining
while true do
if i > leftEnd then break end
udg_MSort_Values[k] = udg_MSort_Left
udg_MSort_Indices[k] = udg_MSort_IndexInitial
k = k + 1
i = i + 1
end
while true do
if j > rightEnd then break end
udg_MSort_Values[k] = udg_MSort_Right[j]
udg_MSort_Indices[k] = udg_MSort_IndexInitial[j]
k = k + 1
j = j + 1
end
-- Update IndexInitial
i = leftStart
while true do
if i == k then break end
udg_MSort_IndexInitial = udg_MSort_Indices
i = i + 1
end
end
-- Recursive Function
---@param start integer
---@param end_ integer
---[USER=121133]@Return[/USER] nothing
function MSort_Merge_Sort(start, end_)
local i = start
local mid
if end_ - start >= 1 then
mid = (end_ + start) / 2
MSort_Merge_Sort(start, mid)
MSort_Merge_Sort(mid + 1, end_)
while true do
if i > mid then break end
udg_MSort_Left = udg_MSort_Values
i = i + 1
end
while true do
if i > end_ then break end
udg_MSort_Right = udg_MSort_Values
i = i + 1
end
MSort_Merge(start, mid, mid + 1, end_)
-- MSort_Display()
-- ^uncomment to display values
end
end
---[USER=121133]@Return[/USER] boolean
function Trig_Merge_Sort_Main()
MSort_Merge_Sort(udg_MSort_StartIndex, udg_MSort_EndIndex)
return false
end
-- ===========================================================================
---[USER=121133]@Return[/USER] nothing
function InitTrig_Merge_Sort()
gg_trg_Merge_Sort = CreateTrigger()
TriggerAddCondition(gg_trg_Merge_Sort, Condition(Trig_Merge_Sort_Main))
end
Last edited: