big-o

Solving the recurrence T(n) = T(n / 2) + O(1) using the Master Theorem? [closed]

不想你离开。 提交于 2020-04-11 18:42:27
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I'm trying to solve a recurrence relation to find out the complexity of an algorithm using the Master Theorem and its recurrences concepts, how can I prove that: T(n) = T(n/2)+O(1) is T(n) = O(log(n)) ? Any explanation would be apprecciated!! 回答1: Your recurrence is T(n) = T(n / 2) + O(1) Since the Master

Time complexity of this primality testing algorithm?

北城余情 提交于 2020-04-05 06:35:26
问题 I have the following code which determines whether a number is prime: public static boolean isPrime(int n){ boolean answer = (n>1)? true: false; for(int i = 2; i*i <= n; ++i) { System.out.printf("%d\n", i); if(n%i == 0) { answer = false; break; } } return answer; } How can I determine the big-O time complexity of this function? What is the size of the input in this case? 回答1: Think about the worst-case runtime of this function, which happens if the number is indeed prime. In that case, the

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;

Finding Big O Notation of For loop which is inside an If condition

空扰寡人 提交于 2020-03-25 18:32:29
问题 sum = 0; for( i = 1; i < n; ++i ) for( j = 1; j < i * i; ++j ) if( j % i == 0 ) for( k = 0; k < j; ++k ) ++sum; How do I find Big O notation for this code? I'm new to this big o notation thing. So I'll appreciate if someone can explain me it simply with details.. Thank you! 回答1: Big O is an asymptotic upper bound of a function. So in your case the the for loops take the most time, if the if condition evaluates always to true, so you can just assume this an get a correct upper bound, which is

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

Analyzing shell sort algorithm (big O)

[亡魂溺海] 提交于 2020-03-21 06:58:12
问题 This is the shell sort algorithm. void shellSort(int array[], int n){ for (int gap = n/2; gap > 0; gap /= 2){ for (int i = gap; i < n; i += 1) { int temp = array[i]; int j; for (j = i; j >= gap && array[j - gap] > temp; j -= gap){ array[j] = array[j - gap]; } array[j] = temp; } } } I'm certain that the outer loop of this algorithm runs logn times but I'm not sure with the middle loop and the innermost loop. This site https://stackabuse.com/shell-sort-in-java/ said that the middle loop runs n

Analyzing shell sort algorithm (big O)

拜拜、爱过 提交于 2020-03-21 06:58:05
问题 This is the shell sort algorithm. void shellSort(int array[], int n){ for (int gap = n/2; gap > 0; gap /= 2){ for (int i = gap; i < n; i += 1) { int temp = array[i]; int j; for (j = i; j >= gap && array[j - gap] > temp; j -= gap){ array[j] = array[j - gap]; } array[j] = temp; } } } I'm certain that the outer loop of this algorithm runs logn times but I'm not sure with the middle loop and the innermost loop. This site https://stackabuse.com/shell-sort-in-java/ said that the middle loop runs n

How does this if statement affect the time complexity?

爷,独闯天下 提交于 2020-03-20 01:40:23
问题 I know that the first two loops are O(n^3), but I am not sure how the if statement affects the time complexity. int sum = 0; for(int i = 1; i < n; i++) { for(int j = 1; j < i * i; j++) { if(j % i == 0) { for(int k = 0; k < j; k++) { sum++; } } } } 回答1: The if statement has a huge impact in the overall complexity of this code. Without if it would be O(n^5) but with if it is O(n^4) . Since j has a range from 1 to i*i and the if will work only when j % i = 0 . That means for each i, j will work

How does this if statement affect the time complexity?

主宰稳场 提交于 2020-03-20 01:37:35
问题 I know that the first two loops are O(n^3), but I am not sure how the if statement affects the time complexity. int sum = 0; for(int i = 1; i < n; i++) { for(int j = 1; j < i * i; j++) { if(j % i == 0) { for(int k = 0; k < j; k++) { sum++; } } } } 回答1: The if statement has a huge impact in the overall complexity of this code. Without if it would be O(n^5) but with if it is O(n^4) . Since j has a range from 1 to i*i and the if will work only when j % i = 0 . That means for each i, j will work