I am trying to use multiple results and track the counter
If you want to do this using multiple results, you will need to modify the recursive call. Each function should return a sorted array along with the number of comparisons needed to sort it:
local s1, n1 = merge_sort(a1) local s2, n2 = merge_sort(a2) local s, n = merge(s1, s2) return s, n1 + n2 + n
Your basic example will look like
return src_array, 0
I think that maybe an additional result is not worth it, and you are better off with a local counter and a nested merge function:
function merge_sort (src_array) local n = 0 -- number of comparisons local function merge(a1, a2) -- merge a1 and a2, incrementing n at each comparison -- return merged array end local function sort(a) if
source share