time-complexity

Calculating the complexity of an algorithm with 3 loops

佐手、 提交于 2020-06-27 11:36:53
问题 I tried to solve the following exercise : What is the order of growth of the worst case running time of the following code fragment as a function of N? int sum = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <= i*i; j++) for (int k = 1; k <= j*j; k++) sum++; and I found that the complexity is O(n 4 ), however the correct answer is : The answer is : N 7 For a given value of i, the body of the innermost loop is executed 1 2 + 2 2 + 3 2 + ... + (i 2 ) 2 ~ 1/3 i 6 times. Summing up over all

Python iter() time complexity?

核能气质少年 提交于 2020-06-27 09:56:27
问题 I was looking up an efficient way to retrieve an (any) element from a set in Python and came across this method: anyElement = next(iter(SET)) What exactly happens when you generate an iterator out of a container such as a set? Does it simply create a pointer to the location of the object in memory and move that pointer whenever next is called? Or does it convert the set to a list then create an iterator out of that? My main concern is if it were the latter, it seems iter() would be an O(n)

Search unsorted array for 3 elements which sum to a value

醉酒当歌 提交于 2020-06-24 16:41:31
问题 I am trying to make an algorithm, of Θ( n² ). It accepts an unsorted array of n elements, and an integer z , and has to return 3 indices of 3 different elements a,b,c ; so a+b+c = z. (return NILL if no such integers were found) I tried to sort the array first, in two ways, and then to search the sorted array. but since I need a specific running time for the rest of the algorithm, I am getting lost. Is there any way to do it without sorting? (I guess it does have to be sorted) either with or

Search unsorted array for 3 elements which sum to a value

Deadly 提交于 2020-06-24 16:41:29
问题 I am trying to make an algorithm, of Θ( n² ). It accepts an unsorted array of n elements, and an integer z , and has to return 3 indices of 3 different elements a,b,c ; so a+b+c = z. (return NILL if no such integers were found) I tried to sort the array first, in two ways, and then to search the sorted array. but since I need a specific running time for the rest of the algorithm, I am getting lost. Is there any way to do it without sorting? (I guess it does have to be sorted) either with or

Time Complexity in singly link list

女生的网名这么多〃 提交于 2020-06-24 14:45:06
问题 I am studying data-structure: singly link list. The website says singly linked list has a insertion and deletion time complexity of O(1) . Am I missing something? website link I do this in C++, and I only have a root pointer . If I want to insert at the end, then I have to travel all the way to the back, which means O(n) . 回答1: The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the

Time Complexity in singly link list

霸气de小男生 提交于 2020-06-24 14:44:49
问题 I am studying data-structure: singly link list. The website says singly linked list has a insertion and deletion time complexity of O(1) . Am I missing something? website link I do this in C++, and I only have a root pointer . If I want to insert at the end, then I have to travel all the way to the back, which means O(n) . 回答1: The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the

Map vs List w.r.t searching Time complexity

坚强是说给别人听的谎言 提交于 2020-06-23 08:25:31
问题 You might have come across someplace where it is mentioned that it is faster to find elements in hashmap/dictionary/table than list/array. My question is WHY? (inference so far I made: Why should it be faster, as far I see, in both data structure, it has to travel throughout till it reaches the required element) 回答1: Let’s reason by analogy. Suppose you want to find a specific shirt to put on in the morning. I assume that, in doing so, you don’t have to look at literally every item of

Map vs List w.r.t searching Time complexity

冷暖自知 提交于 2020-06-23 08:22:48
问题 You might have come across someplace where it is mentioned that it is faster to find elements in hashmap/dictionary/table than list/array. My question is WHY? (inference so far I made: Why should it be faster, as far I see, in both data structure, it has to travel throughout till it reaches the required element) 回答1: Let’s reason by analogy. Suppose you want to find a specific shirt to put on in the morning. I assume that, in doing so, you don’t have to look at literally every item of

What is complexity of this code? (Big O) Is that linear?

99封情书 提交于 2020-06-17 09:41:33
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In

What is complexity of this code? (Big O) Is that linear?

荒凉一梦 提交于 2020-06-17 09:40:09
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In