开发者

Order numbers with Lua

I'm trying to make something to find the median, mode, mean, and range of a set of data. Mean is easy to find using the programming; but median, mode, and range require the numbers to be in order (from least to greatest).

Also, I'm trying to assemble it so it returns the data I would need to make a box and whisker plot. (Not all, just the basic).

Right now I'm simply working on this: Order the numbers into a table (that a function will return)

QWERTYUIOP[]\

Okay, here's the main question: How would I do this?

This is what I'm running on:

function Order_Numbers(Data_Set, Greatest_Integer, Least_Integer)
local Ordered = {} --Give a place for the numbers to go
for i=Least_Integer, Greatest_Integer do --Start from the lowest value, continue to highest.
table.insert(Ordered, Data_Set[i])
end
开发者_运维技巧return Ordered
end

But it doesn't work! Anyone have ideas?


Have you considered using table.sort? It even allows you to provide a function to do the comparison.


If you can sort in place, use table.sort(Data_Set).


The Lua distribution includes sort.lua which has a simple implementation of quick sort; slightly simplified, the core is as follows:

function qsort(vec, low, high)
  if low < high then
    local middle = partition(vec, low, high)
    qsort(vec, low, middle-1)
    qsort(vec, middle+1, high)
  end
end

-> http://lua-users.org/wiki/LazySort

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜