Time Complexity Flashcards
Big O notaqtion
1
Q
What is the time complexity of this function?
public static void printFirstItem(int[] items) {
System.out.println(items[0]);
}
A
This method runs in O(1)O(1) time (or “constant time”) relative to its input.
2
Q
void printAllItems(const vector& items) { for (int item : items) { cout << item << endl; } }
A
This function runs in O(n) time (or “linear time”), where “n” is the number of items in the vector.
3
Q
void printAllPossibleOrderedPairs(const vector& items) { for (int firstItem : items) { for (int secondItem : items) { cout << firstItem << ", " << secondItem << endl; } } }
A
this function runs in O(n^2) time (or “quadratic time”).
4
Q
Average and Worst Time Complexity to Access an Array
A
Average : O(1)
Worst : O(1)