Time Complexity Flashcards
What is time complexity?
Time complexity is a way to measure how fast or slow an algorithm runs based on the size of the input (n).
It helps us understand how the time required for execution grows when the input size increases.
O(1) - Constant Time
The algorithm always takes the same amount of time, no matter the input size
O(log n) - Logarithmic Time
The number of steps reduces by half each time (same as in a Binary Search)
In the worst case it takes O(log2 n) steps.
O(n) - Linear Time
The time increases directly with the input size (for example searching for an element in an unsorted list)
If there are 1000 elements, it may take up to 1000 steps
0(n2)- Quadratic Time
Time grows exponentially as the input increases
For example in a bubble sort, where we compare every element to every other element
Smaller complexity = Faster algorithms T/F
True
Which is better Linear Search O(n) or Binary SearchO(log n)?
Binary Search O(log n)