complexity-theory

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

Create array with length n, initialize all values with 0 besides one which index matches a certain condition, in O(1)?

柔情痞子 提交于 2020-03-05 03:25:51
问题 I want to create an array of type number with length n. All values inside the array should be 0 except the one which index matches a condition. Thats how i currently do it: const data: number[] = []; for (let i = 0; i < n; i++) { if (i === someIndex) { data.push(someNumber); } else { data.push(0); } } So lets say n = 4 , someIndex = 2 , someNumber = 4 would result in the array [0, 0, 4, 0] . Is there a way to do it in O(1) instead of O(n)? 回答1: You need to assign n values, and so there is

Create array with length n, initialize all values with 0 besides one which index matches a certain condition, in O(1)?

空扰寡人 提交于 2020-03-05 03:24:08
问题 I want to create an array of type number with length n. All values inside the array should be 0 except the one which index matches a condition. Thats how i currently do it: const data: number[] = []; for (let i = 0; i < n; i++) { if (i === someIndex) { data.push(someNumber); } else { data.push(0); } } So lets say n = 4 , someIndex = 2 , someNumber = 4 would result in the array [0, 0, 4, 0] . Is there a way to do it in O(1) instead of O(n)? 回答1: You need to assign n values, and so there is

Sorting algorithms for data of known statistical distribution?

时光怂恿深爱的人放手 提交于 2020-02-16 18:47:29
问题 It just occurred to me, if you know something about the distribution (in the statistical sense) of the data to sort, the performance of a sorting algorithm might benefit if you take that information into account. So my question is, are there any sorting algorithms that take into account that kind of information? How good are they? Edit : an example to clarify: if you know the distribution of your data to be Gaussian, you could estimate mean and average on the fly as you process the data. This

Sorting algorithms for data of known statistical distribution?

痴心易碎 提交于 2020-02-16 18:44:48
问题 It just occurred to me, if you know something about the distribution (in the statistical sense) of the data to sort, the performance of a sorting algorithm might benefit if you take that information into account. So my question is, are there any sorting algorithms that take into account that kind of information? How good are they? Edit : an example to clarify: if you know the distribution of your data to be Gaussian, you could estimate mean and average on the fly as you process the data. This

Sorting algorithms for data of known statistical distribution?

旧城冷巷雨未停 提交于 2020-02-16 18:42:20
问题 It just occurred to me, if you know something about the distribution (in the statistical sense) of the data to sort, the performance of a sorting algorithm might benefit if you take that information into account. So my question is, are there any sorting algorithms that take into account that kind of information? How good are they? Edit : an example to clarify: if you know the distribution of your data to be Gaussian, you could estimate mean and average on the fly as you process the data. This

Sorting algorithms for data of known statistical distribution?

橙三吉。 提交于 2020-02-16 18:42:14
问题 It just occurred to me, if you know something about the distribution (in the statistical sense) of the data to sort, the performance of a sorting algorithm might benefit if you take that information into account. So my question is, are there any sorting algorithms that take into account that kind of information? How good are they? Edit : an example to clarify: if you know the distribution of your data to be Gaussian, you could estimate mean and average on the fly as you process the data. This

Complexity to find if there is a missing element in an array

大憨熊 提交于 2020-02-02 05:03:10
问题 I am trying to write a function (in C) that checks if an array has all the elements (between 0 and its "size-1") For example, if the array's size is 3, it should have {0, 1, 2 } in any order. The question is: what is the most efficient complexity to do this without an extra array? The complexity of my attempt, showed below, is (average of nlogn + n). edit: sorry for the miss understanding, any whole number can be an input, which means checking size wont work --> {0, 0, 3} int check_missing

analysing time complexity of my programs

筅森魡賤 提交于 2020-02-02 03:15:30
问题 I have problem in determining time complexities of algorithms. for(int i=0;i <n i++){} O(n) for(int i= 0 ;i<n ;i++){ O(n^2) for(int j=0;j<n;j++){ } } Now for following code whats the complexity for(i =0; i<n ; i++) {} for (j=0;j<n ;j++ ) {} is it O(2n) as it invloves 2 seperate loops? what if i start j =5 to n? 回答1: There is no O(2n) , it's just O(n) . In other words, it scales at the same rate as n increases. If it was a nested loop, it would be O(n 2 ) but the presence of your {} empty