sorting - Merge function in Lua -


Hey, I'm trying to implement merge functions in merge sort in merge. I know algorithms very well, but I am new to Luya. I "wrong to insert" # 1 to the "wrong logic #" (table was found, zero was found) "I believe the error is my recursive Pointing on the call I could not understand it and I feel that this is something very trivial to give me some guidance, just need lua guru thanks.This is my job:

 < Code> merge functions (l1, l2) If # l1 == 0 then come back l2 elseif # l2 == then come back to L1 if L1 [1] & lt; = l2 [1] then TMP = L1 [1] table. Remove (l1, 1) Return table.Instant (Merge (L1, L2), TMP) Other TMP = L2 [1] Table. Revue (l2,1) Return Table. Install (not being fully) (merge (merged ( L1, L2, TMP) End and End  

Ensure that you < What do you want to accomplish with the code> merge function, but a clear problem is that table.ins Ert does not return a table, it returns zero .

> Save the result of the merge, using table.insert using t and returning to the end < Code> T does not seem to break as before.

  function merge (l1, l2) if # l1 == 0 then come back l2 elseif # l2 == 0 then back to L1 and if L1 [1] & lt; = L2 [1] then local tmp = table.remove (l1, 1) local T = merge (l1, l2) table.insert (t, tmp) returns t and local tmp = table.remove (l2, 1) local t = Merge (l1, l2) Table. Instant (T, TMP) Returns T & End  

I did a note on some other modifications. table.remove returns the deleted element, so you do not have to reach the first element and then delete it. I recommend using the keyword local for the variable, because all the variables in Lua are global by default.


Comments