time-complexity

Scenarios for selection sort, insertion sort, and quick sort

隐身守侯 提交于 2019-12-21 06:25:33
问题 If anyone can give some input on my logic, I would very much appreciate it. Which method runs faster for an array with all keys identical, selection sort or insertion sort? I think that this would be similar to when the array is already sorted, so that insertion sort will be linear, and the selection sort quadratic. Which method runs faster for an array in reverse order, selection sort or insertion sort? I think that they would run similarly, since the values at every position will have to be

Scenarios for selection sort, insertion sort, and quick sort

给你一囗甜甜゛ 提交于 2019-12-21 06:23:27
问题 If anyone can give some input on my logic, I would very much appreciate it. Which method runs faster for an array with all keys identical, selection sort or insertion sort? I think that this would be similar to when the array is already sorted, so that insertion sort will be linear, and the selection sort quadratic. Which method runs faster for an array in reverse order, selection sort or insertion sort? I think that they would run similarly, since the values at every position will have to be

Complexity of Hashing

↘锁芯ラ 提交于 2019-12-21 04:28:33
问题 How do we find out the average and the worst case time complexity of a Search operation on Hash Table which has been Implemented in the following way: Let's say 'N' is the number of keys that are required to be hashed. We take a hash table of size M (M=alpha*N, alpha ~ 0.1). Collisions are resolved by storing the keys in a chained linked list fashion, storing each new entry at the head of each linked list pointed to by 'hashTable[i]'. IMHO, the best , avg and worst case complexities could be

What is the complexity of JSON.parse() in JavaScript?

人走茶凉 提交于 2019-12-21 04:07:26
问题 The title says it all. I'm going to be parsing a very large JSON string and was curious what the complexity of this built in method was. I would hope that it's θ(n) where n is the number of characters in the string since it can determine whether there is a syntax error or not. I tried searching but couldn't come up with anything. 回答1: JSON is very simple grammar that does not require even lookaheads. As soon as GC is not involved then it is purely O(n) . 回答2: I do not know of the

Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)?

大憨熊 提交于 2019-12-21 04:00:34
问题 The question is to find out sum of distances between every two nodes of BinarySearchTree given that every parent-child pair is separated by unit distance. It is to be calculated after every insertion. ex: ->first node is inserted.. (root) total sum=0; ->left and right node are inserted (root) / \ (left) (right) total sum = distance(root,left)+distance(root,right)+distance(left,right); = 1 + 1 + 2 = 4 and so on..... Solutions I came up with: Brute-force. Steps: perform a DFS and track all the

Counting all permutations of a string (Cracking the Coding Interview, Chapter VI - Example 12)

谁都会走 提交于 2019-12-20 19:47:22
问题 In Gayle Laakman's book "Cracking the Coding Interview", chapter VI (Big O), example 12, the problem states that given the following Java code for computing a string's permutations, it is required to compute the code's complexity public static void permutation(String str) { permutation(str, ""); } public static void permutation(String str, String prefix) { if (str.length() == 0) { System.out.println(prefix); } else { for (int i = 0; i < str.length(); i++) { String rem = str.substring(0, i) +

Why Time complexity of permutation function is O(n!)

一个人想着一个人 提交于 2019-12-20 19:44:06
问题 Consider following code. public class Permutations { static int count=0; static void permutations(String str, String prefix){ if(str.length()==0){ System.out.println(prefix); } else{ for(int i=0;i<str.length();i++){ count++; String rem = str.substring(0,i) + str.substring(i+1); permutations(rem, prefix+str.charAt(i)); } } } public static void main(String[] args) { permutations("abc", ""); System.out.println(count); } } here the logic, that i think is followed is- it considers each character

Best practices for measuring the run-time complexity of a piece of code

女生的网名这么多〃 提交于 2019-12-20 17:50:13
问题 I have a gnarly piece of code whose time-efficiency I would like to measure. Since estimating this complexity from the code itself is hard, I want to place it in a loop and time the results. Once enough data-points (size -> time) are gathered, I can see which curve fits best. Repeating operations a number of times with random input data of a given size can smooth out fluctuations due to the OS deciding to multitask at bad moments, yielding more precise times. Increasing the size of the

Add to SortedSet<T> and its complexity

旧城冷巷雨未停 提交于 2019-12-20 17:42:18
问题 MSDN states the following SortedSet(T).Add Method : If Count is less than the capacity of the internal array, this method is an O(1) operation. Could someone please explain "how so"? I mean when adding new value we need to find a correct place to add a value (comparing it with another values) and internal implementation looks like a "Red-Black tree" with O (log N) insertion complexity. 回答1: The comment is simply wrong. Yes, it is a red-black tree, O(log(n)) for inserts. Taking a look with

Add to SortedSet<T> and its complexity

白昼怎懂夜的黑 提交于 2019-12-20 17:40:06
问题 MSDN states the following SortedSet(T).Add Method : If Count is less than the capacity of the internal array, this method is an O(1) operation. Could someone please explain "how so"? I mean when adding new value we need to find a correct place to add a value (comparing it with another values) and internal implementation looks like a "Red-Black tree" with O (log N) insertion complexity. 回答1: The comment is simply wrong. Yes, it is a red-black tree, O(log(n)) for inserts. Taking a look with