So I ended up using a sort of iterative binary sort algorithm that best suits my needs. The function will compare the value to the median between the lowest and highest points on the table that the value could possibly be between. Every iteration the range is adjusted accordingly.
In the event that the number being sorted is compared to an equal value, the function will treat it as though the number being sorted is larger. This is intended, because I wrote this code to function inside my own 'Task Executer'. The Task Executer is a class which sequentially calls a list of functions, ordered by priority. The elements in the code below simulate that priority. I wanted consistent behavior in the event that several functions are sorted under the same priority, so the most recently sorted function will execute last.
I also replaced some library functions with my own code to improve efficiency. I plan on using my Task Executer for a fire spreading mechanic in my survival game, along with several GUI animations, so efficiency is very important to me since this code will get plenty of action.
CODE:
local arr = {1, 1, 2, 3, 5, 8, 13, 21, 34}
local function sort(value)
local low = 0
local high = #arr + 1
local pivot
while high - low ~= 1 do
pivot = (low + high)/2; pivot = pivot - pivot % 1
if value < arr[pivot] then
high = pivot
else
low = pivot
end
end
for index = #arr + 1, high + 1, -1 do
arr[index] = arr[index - 1]
end; arr[high] = value
print("SORTING VALUE " .. value .. " :", unpack(arr))
end
print("ORIGINAL:", unpack(arr))
sort(4)
sort(4)
sort(8)
sort(0)
sort(34)
sort(93)
OUTPUT:
ORIGINAL: 1 1 2 3 5 8 13 21 34
SORTING VALUE 4: 1 1 2 3 4 5 8 13 21 34
SORTING VALUE 4: 1 1 2 3 4 4 5 8 13 21 34
SORTING VALUE 8: 1 1 2 3 4 4 5 8 8 13 21 34
SORTING VALUE 0: 0 1 1 2 3 4 4 5 8 8 13 21 34
SORTING VALUE 34: 0 1 1 2 3 4 4 5 8 8 13 21 34 34
SORTING VALUE 93: 0 1 1 2 3 4 4 5 8 8 13 21 34 34 93
I think there's still some minimal room for improvement with optimizations, but this is probably good enough for now. Thanks for the help and resources guys!
9 comments
Are the elements in the array going to be sorted when they are inserted?
@MrLonely1221 Yes
Is there a reason to not use table.sort()?
@ xPolariumI don't want to sort the entire table, just one element.
If the whole table is going to be sorted in the long run because you're inserting elements in a sorted fashion, why not just sort the table?
Your answer is very good @aquathorn321. The fastest way to place an element in the sorted array would be to binary search the location and inserting it, which you seem to do that in your recursive sort. You will not get a time complexity better than O(log n) for placing it might i add. Recall that sorting typically takes O(n log n). Therefore, that is the fastest way.
there is no 'best sorting algorithm' each have their pros and cons. if you have small arrays like this it wont matter anyway
@AbstractionsReality Thanks I didn't know anything about sorting algorithms so I just came up with my own. I'm glad I found the one best suited for my purposes lol
@royaltoe I guess I could've phrased my problem better. The table I provided was just an example. I plan for the table to take on a much larger and dynamic size, and I will be sorting multiple elements between frames. Instead of best I'm looking for the method which is most efficient on average, with efficiency measured by how long it takes the code to execute.