Big O: O(n)
23 April 2023 (Updated 23 April 2023)
In a nutshell
An algorithm is considered O(n)
when its complexity (i.,e., number of operations) is directly proportional to the input size.
export function printNumbersUpTo(n: number): void {
for (let i = 1; i <= n; i++) {
console.log(i);
}
}
printNumbersUpTo(10)
printNumbersUpTo(100)
Here we have a single for
loop that will run n
times. If you increase n
by 1, you increase the number of loop operations by 1. If you increase it by 10, you increase the loop operations by 10. The number of operations is roughly proportionate to n
.
Tagged:
Big O Notation
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment