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
1 answer

Code that Parses through Pseudo Code for Big O Analysis

I'm currently trying to code a program that reads pseudo code from a txt file and outputs the Big O notation for the pseudo code as a new txt file. I'm struggling to find a way to parse through the for loops for the key information needed to find…
3
votes
3 answers

Time complexity of LinkedList.subList(int, int)

If I have a linked list of objects and I want the sublist from index 2 to 5. Is this operation o(1)? All you would need to do is null the reference to prev on the node at index 2, and return the node at index 2, correct? Does this require copying…
3
votes
2 answers

Best Data Structure for searching a coordinate in a 2D grid of categories

I want to search a coordinate in a 2D grid in which each element stores the horizontal and vertical coordinate limits and return a corresponding . For Example: let the coordinate to be searched be (15,25) and the Grid be (where A,B,C,D,E and F are…
3
votes
0 answers

Image processing in matlab: rearranging operations to maximize performance in a series of convolutions?

I'm interested in implementing a particular problem in matlab, and I have the following code with a 'naive' implementation. I include typical values of sizes of the variables. l = 99; m = 21; n = 5; s1 = 1024; s2 = 1024; H =…
3
votes
1 answer

Time complexity of matrix multiplication

I have a trouble in understanding time complexity. People can look at algorithms and directly say what its time complexity is, but I can't do that well. Consider two n * n matrices (A and B). Their multiplication result is C. So, value of C11…
rakamakafo
  • 1,144
  • 5
  • 21
  • 44
3
votes
1 answer

Worst scenario for shell sort: Θ(N^3/2) or O((NlogN)^2)?

I am looking for worst case of Shell sort. According to this, the worst case is O(N^3/2) but here, it is claimed that the worst case is O((N log N)^2)). I think that worst case should be a a sequence containing largest vales in odd positions.…
David
  • 1,343
  • 3
  • 12
  • 13
3
votes
3 answers

Calculating the Time Complexity of nested loop with logarithmic increase

I am learning at my own pace online. I was solving some examples but I can't wrap my mind around this one: while(i
SarpSTA
  • 279
  • 2
  • 15
3
votes
1 answer

PHP OpenDir vs array_rand

I have a directory of images, this could contain anywhere from 100 to many thousands of images. I need to take a sample of 81 random images out of this directory to be used (in an array). I am currently using the following to grab an…
Zac Warham
  • 158
  • 11
3
votes
1 answer

Time compexity of palindrome recursive algorithm

I have written this recursive function to find the palindrome. def palindrome(string): print("palindrome called with:"+string) if(len(string)<=3): return string[0]==string[-1] else: res=palindrome(string[1:-1]) …
sheldon cooper
  • 455
  • 2
  • 8
  • 22
3
votes
1 answer

{Java - PriorityQueue} time complexity of this code

Given an array containing N points find the K closest points to the origin (0, 0) in the 2D plane. You can assume K is much smaller than N and N is very large. E.g: given array: (1,0), (3,0), (2,0), K = 2 Result = (1,0), (2,0) …
Peter
  • 155
  • 2
  • 10
3
votes
1 answer

simple time complexity O(nlogn)

I am reviewing some Big O notation for an interview and I come across this problem. for i = 1 to n do: j = i while j < n do: j = 2 * j simple right? the outer loop provides n steps. and each of those steps we do a single step O(1) of…
OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
3
votes
2 answers

Priority Queue poll() time complexity

Given the code below: pq.offer(x); pq.poll(); For the first line code, element x is inserted into Priority Queue pq, the time complexity of the offer is log(k), where k is the size of pq. Then my question is, for the second line code that…
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
3
votes
1 answer

K'th Min From a set of intervals

You have given a set of intervals like {2,7} , {3,8}, {9,11} , {-4,-1} so on. The question is to find the k'th min from these set of intervals. Also the duplicates are counted twice. For example if intervals are {1,4} and {2,6} and k = 3 then the…
Avishek Bhattacharya
  • 6,534
  • 3
  • 34
  • 53
3
votes
2 answers

What is the complexity of sum of log functions

I have an algorithm that have the following complexity in big O: log(N) + log(N+1) + log(N+2) + ... + log(N+M) Is it the same as log(N+M) since it is the largest element? OR is it M*log(N+M) because it is a sum of M elements?
yerzhan7
  • 185
  • 2
  • 14
3
votes
1 answer

Triple Nested Big O with dependent indices

I am working on finding the Big O to this problem, and I am having difficulty with the third nested loop. Here is the code for (int i = 1 to n) for (int j = i to n) for (int k = j*j to n) //some constant operation The i forloop is…
1 2 3
99
100