time-complexity

overall time complexity of the program?

帅比萌擦擦* 提交于 2020-04-16 02:05:44
问题 suppose you take 'n' inputs in an array(and for that you have to run a loop that iterates 'n' times for 'n' different locations), which would have O(n) complexity. And then you you try to perform operations that have O(log n) or less than O(n) time complexity. my question here is that does it really matter to have complexity of those operations less than O(n) since your whole program will have atleast O(n) time complexity. 回答1: Indeed, a program's time complexity could be dominated by the

Optimization, time complexity and flowchart (Scilab)

你离开我真会死。 提交于 2020-03-26 03:22:01
问题 I tried to optimize this code, but it’s impossible to optimize anymore. Please help with building a flowchart for this algorithm. A = [-1,0,1,2,3,5,6,8,10,13,19,23,45]; B = [0,1,3,6,7,8,9,12,45]; N1 = length(A); N2 = length(B); t = 1; m = 10; C = []; for i=1:N1 for j=1:N2 if A(i)==B(j) break else if j==N2 C(t)=A(i); t=t+1; end end end end disp(C); N3=length(C); R = []; y = 1; for l=1:N3 if C(l)>m R(y)=C(l); y=y+1; end end disp(R); How to find time complexity of an algorithm I think it should

Turning off Compiler Optimizations? My C# code to evaluate order of algorithm is returning logN or N^3 instead of N for simple loop

好久不见. 提交于 2020-03-25 19:11:27
问题 As a learning exercise, I am writing a library to evaluate the complexity of an algorithm. I do this by seeing how long the alorgorithm takes for given inputs N, and then do a polynomial regression to see if the algorithm is best fitted by N, N^2, log(N), etc. I wrote Unit Test cases and they seem to work for N^2 and LogN. It's the simplest case, N that is giving me grief. For an order N algorithm, I'm using the following: uint LinearAlgorithm2(uint n) { uint returnValue = 7; for (uint i = 0;

Turning off Compiler Optimizations? My C# code to evaluate order of algorithm is returning logN or N^3 instead of N for simple loop

南楼画角 提交于 2020-03-25 19:11:13
问题 As a learning exercise, I am writing a library to evaluate the complexity of an algorithm. I do this by seeing how long the alorgorithm takes for given inputs N, and then do a polynomial regression to see if the algorithm is best fitted by N, N^2, log(N), etc. I wrote Unit Test cases and they seem to work for N^2 and LogN. It's the simplest case, N that is giving me grief. For an order N algorithm, I'm using the following: uint LinearAlgorithm2(uint n) { uint returnValue = 7; for (uint i = 0;

Does time complexity changes as we change our programming language?

浪子不回头ぞ 提交于 2020-03-25 18:01:46
问题 In Python, if we need to find the maximum element from the list. We use : >>>max(listname) to get the maximum number from the list. Time Complexity : O(1) If we use C/C++, we need to iterate over the loop and get the max. Time Complexity: O(n) or O(n square) Hence, does the time complexity changes over the programming language? 回答1: No. max(listname) is always O(N) where N is the length of listname (*). This is just by definition of complexity - because the iteration has to happen somewhere -

Does time complexity changes as we change our programming language?

梦想的初衷 提交于 2020-03-25 18:01:26
问题 In Python, if we need to find the maximum element from the list. We use : >>>max(listname) to get the maximum number from the list. Time Complexity : O(1) If we use C/C++, we need to iterate over the loop and get the max. Time Complexity: O(n) or O(n square) Hence, does the time complexity changes over the programming language? 回答1: No. max(listname) is always O(N) where N is the length of listname (*). This is just by definition of complexity - because the iteration has to happen somewhere -

Searching triplets in two arrays

情到浓时终转凉″ 提交于 2020-03-25 17:57:58
问题 Given two arrays a and b of size n . I have to choose one element from a and two elements from b such that. a[i] * a[i] = b[j] * b[k] . I am able to find BruteForce in O(n^2 log(n)) traversing both the arrays and binary searching the value. But dont know how to improve it even more. How do I count all these number where elements range from 1 <= a[i] <= 10^6 and 1 <= n <= 10^6 . 回答1: Well, I can think in a O(n²) solution. unordered_multiset S = {} for each element B in b { S = S ∪ B } First of

Searching triplets in two arrays

亡梦爱人 提交于 2020-03-25 17:56:18
问题 Given two arrays a and b of size n . I have to choose one element from a and two elements from b such that. a[i] * a[i] = b[j] * b[k] . I am able to find BruteForce in O(n^2 log(n)) traversing both the arrays and binary searching the value. But dont know how to improve it even more. How do I count all these number where elements range from 1 <= a[i] <= 10^6 and 1 <= n <= 10^6 . 回答1: Well, I can think in a O(n²) solution. unordered_multiset S = {} for each element B in b { S = S ∪ B } First of

Reducing the time complexity of an algorithm with nested loops

别来无恙 提交于 2020-03-23 08:00:11
问题 I have the following algorithm which I want to rewrite so it has time complexity O(n). I am new to algorithms but from my understanding since the two for loops both do a multiple of n iterations, the complexity will always be O(n 2 ). Is it even possible to reduce the complexity of this? Algorithm example(ArrayA, ArrayB, n) Input: 2 arrays of integers, ArrayA and ArrayB, both length n Output: integer value <- 0 1 operation for i <- 0 to n-1 n-1 operations for j <- 0 to n-1 (n-1)^2 operations

Why does my sieve not perform well for finding primes?

故事扮演 提交于 2020-03-22 09:05:15
问题 I wrote two prime finder functions and the sieve only performs about 10% better. I'm using two optimizations for the simple version. Don't check even numbers Only check up to the square root or j * j <= i . ( equivalent ) and one optimization for the sieve version Only check up to the square root or i * i <= n . ( equivalent ) What optimizations can I add to the sieve? My sieve is pretty slow. I don't want to do a bitwise implementation yet, I want to understand if this implementation offers