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