time-complexity

Running Time For Insertion Sort

↘锁芯ラ 提交于 2019-12-31 04:33:09
问题 for (int p=1; p < a.size(); p++) { int tmp = a[p]; for(j=p; j>0 && tmp < a[j-1]; j--) { a[j] = a[j-1]; } a[j] = tmp; } I'm having trouble figuring out the worse case for Insertion sort. So, the array given is in descending order, and we want to sort it in ascending order. The outer loop goes through the array. So, it runs (n times). O(n) int tmp=a[p] ---- This statement gets executed n times. O(n) The inner loop get executed (1+2+3+4+5+6+.... +n-1) times. O(n^2) a[j]= tmp -------- This

Running time/time complexity for while loop with square root

半腔热情 提交于 2019-12-31 03:39:04
问题 This question looks relatively simple, but I can't seem to find the running time in terms of n. Here is the problem: j = n; while(j >= 2) { j = j^(1/2) } I don't really need the total running time, I just need to know how to calculate the amount of times the second and third lines are hit (they should be the same). I'd like to know if there is some sort of formula for finding this, as well. I can see that the above is the equivalent of: for(j = n; n >= 2; j = j^(1/2) Please note that the type

Time Complexity of “in” (containment operator)

南楼画角 提交于 2019-12-31 02:40:46
问题 I was just wondering when understanding the time complexity of an algorithm like the one below. For a python list, if we have a for loop iterating over it, and then a containment check, would the time complexity of that be O(n^2). I know both are O(n) (or I think) so having them nested in one another would that make it O(n^2)? I think if this "list" is actually a list, then the time complexity of the code below is O(n^2). But if it's a dictionary it would be O(n) because lookup is O(1). Is

Can this python code be more efficient?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 01:56:31
问题 I have written some code to find how many substrings of a string are anagram pairs. The function to find anagram(anagramSolution) is of complexity O(N). The substring function has complexity less than N square. But, this code here is the problem. Can it be more optimized? for i in range(T): x = raw_input() alist = get_all_substrings(x) for k, j in itertools.combinations(alist,2): if(len(k) == len(j)): if(anagramSolution(k,j)): counter +=1 counterlist.append(counter) counter = 0 The alist can

What is time complexity for the following code?

主宰稳场 提交于 2019-12-30 16:03:19
问题 It seems the complexity of the following code should be O(n^2) but it's O(n), how? void fun(int n, int arr[]) { int i = 0, j = 0; for(; i < n; ++i) while(j < n && arr[i] < arr[j]) j++; } 回答1: j is not reset to 0 with every iteration of the outer loop. As such, it runs to n-1 just once, same as i does. So you have two parallel/intermingled iterations from 0 to (at most) n-1 . In every step, the program increases i by one. The program terminates when i reaches n . The "outer loop" is run n

How enhanced is enhanced-for loop?

寵の児 提交于 2019-12-30 11:09:07
问题 I am iterating on the elements of a list of String objects one after the other: LinkedList list; // add values to the list here for (int i = 0; i < list.size(); i++) System.out.println(list.get(i)); Here, each and every time i invoke get() on list, the list is iterated from one of its ends all the way to the i-th element-- so the complexity of the above loop is O(n^2). Is is a.) the same as above for enhanced-for loop, or b.) is for-loop maintaining the pointer where it's last have been and

How many times will the while loop be executed?

半世苍凉 提交于 2019-12-30 09:40:13
问题 I am wondering about how many times this while loop would execute. This is a function that adds two numbers using XOR and AND. def Add(x, y): # Iterate till there is no carry while (y != 0): # carry now contains common # set bits of x and y carry = x & y # Sum of bits of x and y where at # least one of the bits is not set x = x ^ y # Carry is shifted by one so that # adding it to x gives the required sum y = carry << 1 return x `` 回答1: Algorithm for No Carry Adder : function no_carry_adder(A

What the iteration cost on a HashSet also depend on the capacity of backing map?

限于喜欢 提交于 2019-12-30 06:19:16
问题 From the JavaDocs of HashSet: This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too

Algorithmic complexity of naive code for processing all consecutive subsequences of a list: n^2 or n^3?

那年仲夏 提交于 2019-12-29 19:23:11
问题 I'm studying for a test and found this question: I can't really determine the complexity, I figured it's either O(n 2 ) or O(n 3 ) and I'm leaning towards O(n 3 ). Can someone tell me what it is and why? My idea that it's O(n 2 ) is because in the j loop, j = i which gives a triangle shape, and then the k loop goes from i + 1 to j , which I think is the other half of the triangle. public static int what(int[] arr) { int m = arr[0]; for (int i=0; i<arr.length; i++) { for (int j=i; j<arr.length

Why do we use linear probing in Hash tables when there is separate chaining linked with lists?

六眼飞鱼酱① 提交于 2019-12-29 11:36:08
问题 I recently learned about different methods to deal with collisions in hash tables. And saw that the separate chaining with linked lists is always more time efficient, and for space efficiency we allocate a predefined memory for Linear probing which later on we might not use,for separate chaining we utilize memory dynamically, so is separate chaining with linked list not more efficient than Linear probing?if yes why do we then use Linear probing at all? 回答1: I'm surprised that you saw chained