Questions tagged [time-complexity]

The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n^3 + 3n, the asymptotic time complexity is O(n^3).

Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Thus the amount of time taken and the number of elementary operations performed by the algorithm differ by at most a constant factor.

Since an algorithm may take a different amount of time even on inputs of the same size, the most commonly used measure of time complexity, the worst-case time complexity of an algorithm, denoted as T(n), is the maximum amount of time taken on any input of size n. Time complexities are classified by the nature of the function T(n).

For instance, an algorithm with T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2^n) is said to be an exponential time algorithm.

Useful links:

Related tags:

10064 questions
3
votes
2 answers

Big-O and Omega Notations

I was reading this question Big-O notation's definition. But I have less than 50 reputation to comment, so I hope someone help me. My question is about this sentence: There are many algorithms for which there is no single function g such that the…
O-BL
  • 326
  • 3
  • 12
3
votes
1 answer

Erlang flatten function time complexity

I need a help with following: flatten ([]) -> []; flatten([H|T]) -> H ++ flatten(T). Input List contain other lists with a different length For example: flatten([[1,2,3],[4,7],[9,9,9,9,9,9]]). What is the time complexity of this function? And…
Sokolik
  • 33
  • 4
3
votes
3 answers

Does this solve 3SUM in O(N log(N)) time?

Does this algorithm work as a solution for 3SUM in O(N log(N)) where the problem is defined by Wikipedia as In computational complexity theory, the 3SUM problem asks if a given set of n real numbers contains three elements that sum to…
3
votes
3 answers

Calculate Big Theta Notation Bubble Sort

I'm trying to calculate the theta notation for bubble sort but I'm getting stuck. Given this procedure (pseudocode): procedure BUBBLE_SORT(A,n) { array A(1 to n) for (int i = 1; i <= n; i++) { for(int j = 1; j <= n-1; j++) { …
zuzu
  • 401
  • 2
  • 6
  • 17
3
votes
3 answers

Sorting n strings using Merge sort

A list of n strings each of length n is sorted into lexicographic order using the merge sort algorithm. The worst case running time of this computation is? I searched this question and everywhere I found the answer to be: O(n2logn). While my…
Rishabh Gupta
  • 389
  • 6
  • 19
3
votes
2 answers

Time complexity of recursive function inside for loop

If we have a function:- int x=0; int fun(int n) { if(n==0) return 1; for(int i=0; i
Zephyr
  • 1,521
  • 3
  • 22
  • 42
3
votes
2 answers

How to find the time complexity of dfs+backtracking algorithms?

I am trying to solve this problem on leetcode https://leetcode.com/problems/factor-combinations/description/ Numbers can be regarded as product of its factors. For example 8 = 2 x 2 x 2; = 2 x 4. Write a function that takes an integer n and…
KBR
  • 464
  • 1
  • 7
  • 24
3
votes
1 answer

Time and Space Complexity of list to str conversion in Python

Trying to find out what is the time complexity of casting to string str([1,2,6,...,3,6]) Pretty sure it's O(1) Not sure how to verify. Edit: about space complexity, That should not be linear to list size, thinking O(1) because string has max…
WebQube
  • 8,510
  • 12
  • 51
  • 93
3
votes
2 answers

Removing Invalid Parentheses - Leetcode Time Complexity

I was solving this problem on leetcode and the problem statement is as follows. Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other…
Srivathsan
  • 600
  • 3
  • 9
  • 19
3
votes
2 answers

reduce the time complexity for python for loop

I have python data frame with the following columns: Index([u'Academic Period', u'Academic Period Desc', u'Student ID', u'Subject', u'Course Number', u'Course Reference Number', u'Course Identification', u'Schedule Type',…
Sophie
  • 77
  • 1
  • 2
  • 8
3
votes
2 answers

sorting algorithm with least time complexity for already sorted array?

I got a question that which type of sorting algorithm will have least time complexity when we are given an already sorted array.
Achyut Dalai
  • 41
  • 1
  • 6
3
votes
3 answers

Why doesn't the additive constant in Big-O notation matter?

In definition of Big-O notation we care only about C coefficient: f(n) ≤ Cg(n) for all n ≥ k Why don't we care about A as well: f(n) ≤ Cg(n) + A for all n ≥ k
rset_d
  • 109
  • 1
  • 1
  • 5
3
votes
2 answers

Determining the Big-O of this algorithm

I'm trying to understand what the Big-O is of the code below. What the code is supposed to do Essentially, I am trying to select a subset (max size = selectionSize) of random nodes and any edges that exists between them. The selection of random…
Moody
  • 851
  • 2
  • 9
  • 23
3
votes
2 answers

Quicksort complexities in depth

So I am having an exam, and a big part of this exam will be quicksort algorithm. As everyone knows, the best case scenario and actually an average case for this algorithm is: O(nlogn). The worst case scenario would be O(n^2). As for the worst case…
3
votes
1 answer

ES6 Maps and Sets: how are object keys indexed efficiently?

In ES6, Maps and Sets can use Objects as keys. However since the ES6 specification does not dictate the underlying implementation of these datastructures, I was wondering how does the modern JS engines store the keys in order to guarantee O(1) or at…
luanped
  • 3,178
  • 2
  • 26
  • 40
1 2 3
99
100