Search code examples
algorithmbig-ocomputer-science

Big O of fixed size array linear search


const a = Array.apply(null, Array(50)).map((x, i) => i);

This array will never be changed, it will always contain 50 elements.

Would a.includes(x) (linear search) be O(n) OR O(50) OR technically O(50) but we call is O(n)


Solution

  • It can't be O(N) because N implies that there is a certain variable that is affecting the runtime. Since the array is always 50 elements it will always loop through 50 times instead of a variable amount of times- so that function is O(50), which we usually simplify to call O(1)- which represents all constant-time functions.