Hello, I recently learned about Data Structures from freeCodeCamp, there I learned about all the various types that a table can be and how keys/indexes could be searched efficiently by analyzing the table through BigO notation or time complexity equation. So that got me wondering what type of search algorithm is used when calling a key/index from a table
Sorry if I don’t know, it’s just Lua or Luau is the first programming language I have written code in before I ever took any computer science courses.
Is linear search being use when doing…
local foo = {1,2,3,4,5,6,7,8}
local i = foo[5]
-- which is equal to this function
local i = function(index)
for i,v in ipairs(foo) do
if i == index then
return v
end
end
end
That means it starts from the top of the table and goes down until it finds the 5th index.
or like in Binary Search finds the middle index of the table and checks if its the 5th index if not, it checks if 5 is above the middle index if true remove half of the table to be scanned and focus on the part that has the 5.
local foo = {1,2,3,4,5,6,7,8}
local i = foo[5]
-- is equal to
foo = {1,2,3,4,5,6,7,8} -- 4 is the middle index, 5 is above 4
foo = {4,5,6,7,8} -- Removes indexes that doesn't have 5, foo{1,2,3} removed.
foo = {4,5,6} -- foo{6,7,8} doesn't have 5.
foo = 5 -- index 5 is in the middle
Imagine foo had 119 Million indexes. Now imagine the name of foo is replaced with RobloxUsers.