big-o

Why is the complexity of simple string concatenation O(n^2)?

自闭症网瘾萝莉.ら 提交于 2021-02-04 21:45:39
问题 I read on several manuals and online sources that the running time of "simple string concatenation" is O(n^2)? The algorithm is this: we take the first 2 strings, create a new string, copy the characters of the 2 original strings in the new string, and repeat this process over and over again until all strings are concatenated. We are not using StringBuilder or similar implementations: just a simple string concatenation. I think the running time should be something like O(kn) where k = number

Why is the complexity of simple string concatenation O(n^2)?

社会主义新天地 提交于 2021-02-04 21:44:07
问题 I read on several manuals and online sources that the running time of "simple string concatenation" is O(n^2)? The algorithm is this: we take the first 2 strings, create a new string, copy the characters of the 2 original strings in the new string, and repeat this process over and over again until all strings are concatenated. We are not using StringBuilder or similar implementations: just a simple string concatenation. I think the running time should be something like O(kn) where k = number

Is this n-body problem O(n^2) or O(n log n)?

旧巷老猫 提交于 2021-02-04 21:33:20
问题 I'm writing an article on the n-body problem, and I'd like to be technically accurate. The code is here. And here are the comments and loops: /** * Given N bodies with mass, in a 3d space, calculate the forces of gravity to be applied to each body. * * This function is exported to JavaScript, so only takes/returns numbers and arrays. * For N bodies, pass and array of 4N values (x,y,z,mass) and expect a 3N array of forces (x,y,z) * Those forcess can be applied to the bodies mass to update the

Is this n-body problem O(n^2) or O(n log n)?

三世轮回 提交于 2021-02-04 21:33:18
问题 I'm writing an article on the n-body problem, and I'd like to be technically accurate. The code is here. And here are the comments and loops: /** * Given N bodies with mass, in a 3d space, calculate the forces of gravity to be applied to each body. * * This function is exported to JavaScript, so only takes/returns numbers and arrays. * For N bodies, pass and array of 4N values (x,y,z,mass) and expect a 3N array of forces (x,y,z) * Those forcess can be applied to the bodies mass to update the

Java program to create a Descending Order using bucket sort

我与影子孤独终老i 提交于 2021-01-29 07:11:55
问题 I was making a program to take 10 user input values(1-100) and use bucket sort to sort it according to the user preference whether by ascending or descending order. I was able to create the ascending order. This is my code for ascending, but how could I make it descending? public class BucketSort{ Scanner in = new Scanner(System.in); public void bucketSort(int array[], int numBuckets){ System.out.println("Choose Sorting Order:"); System.out.println("[A] Ascending \n[D] Descending \n Enter

Leibniz determinant formula complexity

时光总嘲笑我的痴心妄想 提交于 2021-01-28 03:03:11
问题 I wrote some code that calculates the determinant of a given nxn matrix using Leibniz formula for determinants. I am trying to figure out it's complexity in O-notation. I think it should be something like: O(n!) * O(n^2) + O(n) = O(n!*n^2) or O((n+2)!) Reasoning: I think O(n!) is the complexity of permutations. and O(n) the complexity of perm_parity, and O(n^2) is the multiplication of n items each iteration. this is my code: def determinant_leibnitz(self): assert self.dim()[0] == self.dim()

Leibniz determinant formula complexity

[亡魂溺海] 提交于 2021-01-28 00:31:12
问题 I wrote some code that calculates the determinant of a given nxn matrix using Leibniz formula for determinants. I am trying to figure out it's complexity in O-notation. I think it should be something like: O(n!) * O(n^2) + O(n) = O(n!*n^2) or O((n+2)!) Reasoning: I think O(n!) is the complexity of permutations. and O(n) the complexity of perm_parity, and O(n^2) is the multiplication of n items each iteration. this is my code: def determinant_leibnitz(self): assert self.dim()[0] == self.dim()

Adjacency list with O(1) look up time using HashSet?

会有一股神秘感。 提交于 2021-01-27 14:32:17
问题 In my algorithms class I've been told that a draw back of Adjacency Lists for graph representation is the O(n) look up time for iterating through the array of adjacent nodes corresponding to each node. I implement my adjacency list by using a HashMap that maps nodes to a HashSet of their adjacent nodes, wouldn't that only take O(1) look up time? Is there something I'm missing? 回答1: As you know look up for value using key in HashMap is O(1). However, in adjacency list the value of the HashMap

Quicksort to already sorted array

大兔子大兔子 提交于 2021-01-24 09:39:08
问题 In this question: https://www.quora.com/What-is-randomized-quicksort Alejo Hausner told in: Cost of quicksort, in the worst case , that Ironically, if you apply quicksort to an array that is already sorted, you will get probably get this costly behavior I cannot get it. Can someone explain it to me. https://www.quora.com/What-will-be-the-complexity-of-quick-sort-if-array-is-already-sorted may be answer to this, but that did not get me a complete response. 回答1: The Quicksort algorithm is this:

What's the computational cost of count operation on strings Python?

三世轮回 提交于 2021-01-22 05:41:40
问题 For example: 'hello'.count('e') Is this O(n)? I'm guessing the way it works is it scans 'hello' and increments a counter each time the letter 'e' is seen. How can I know this without guessing? I tried reading the source code here, but got stuck upon finding this: def count(s, *args): """count(s, sub[, start[,end]]) -> int Return the number of occurrences of substring sub in string s[start:end]. Optional arguments start and end are interpreted as in slice notation. """ return s.count(*args)