big-o

Time Complexity Nested Loop Inner Loop + Outer Loop

只谈情不闲聊 提交于 2020-01-24 09:31:47
问题 Can anyone explain what the time complexity is for this algorithm? for (i = 1; i <= n; i++){ for(j = 1; j <= n; j += i) { // note: not j++ printf("Iteration %d : %d\n", i, j); } } 回答1: The printf in the inner loop is called exactly ceil(n) + ceil(n/2) + ceil(n/3) + ... ceil(n/n) times. To get rid of ceil , we know that ceil(y/n) is bounded above by y/n + 1 , so we know that the number of executions is >= n + n/2 + n/3 ... n/n but is < n + 1 + n/2 + 1 + n/3 + 1 + n/4 + 1... + n/n + 1 . The

Solving the recurrence T(n) = 2T(sqrt(n))

别来无恙 提交于 2020-01-24 06:00:23
问题 I would like to solve the following recurrence relation: T(n) = 2T(√n); I'm guessing that T(n) = O(log log n) , but I'm not sure how to prove this. How would I show that this recurrence solves to O(log log n) ? 回答1: One idea would be to simplify the recurrence by introducing a new variable k such that 2 k = n. Then, the recurrence relation works out to T(2 k ) = 2T(2 k/2 ) If you then let S(k) = T(2 k ), you get the recurrence S(k) = 2S(k / 2) Note that this is equivalent to S(k) = 2S(k / 2)

Can you sort n integers in O(n) amortized complexity?

孤者浪人 提交于 2020-01-21 06:41:25
问题 Is it theoretically possible to sort an array of n integers in an amortized complexity of O(n)? What about trying to create a worst case of O(n) complexity? Most of the algorithms today are built on O(nlogn) average + O(n^2) worst case. Some, while using more memory are O(nlogn) worst. Can you with no limitation on memory usage create such an algorithm? What if your memory is limited? how will this hurt your algorithm? 回答1: Any page on the intertubes that deals with comparison-based sorts

Big-O Notation regarding logarithms

混江龙づ霸主 提交于 2020-01-21 05:28:13
问题 I got asked an interview question that wanted me to discern the Big-O notation of several logarithmic functions. The functions were as follows: f(x) = log 5 (x) f(x) = log(x 5 ) f(x) = log(6*log x) f(x) = log(log x) I was told that the Big-O for the first and second are not equivalent and the third and fourth are not equivalent after mistakenly guessing the opposite. Can anyone explain why they are not equivalent and what their Big-O are then? 回答1: log 5 x is the same as writing log log log

How can merge sort have multiple big-oh values?

随声附和 提交于 2020-01-16 00:36:43
问题 In What exactly does big Ө notation represent?, the most upvoted answer contains the following statement: For example, merge sort worst case is both O(n*log(n)) and Omega(n*log(n)) - and thus is also Ө(n*log(n)) , but it is also O(n^2) , since n^2 is asymptotically "bigger" than it. However, it is not Ө(n^2) , Since the algorithm is not Omega(n^2) . I have two questions: How do you determine that the worst case is O(n*log(n)) and Omega(n*log(n)) . In "Introduction to Algorithms" you determine

Alternatives to nested foreach when comparing list contents based on some (not all) property values

我们两清 提交于 2020-01-15 03:21:38
问题 As part of this question, it was pointed out repeatedly that I had an O(n^2) problem using code similar to this... public class Foo { public string IdentityValue {get;set;} public string Prop1 {get;set;} public string Prop2 {get;set;} } List<Foo> itemSet1 = GenerateLargeItemSet(); //makes a large list, > 5000 items for example List<Foo> itemSet2 = GenerateLargeItemSet(); foreach (var itemFromSet1 in itemSet1) { //does a corresponding item exist in itemSet2? var itemSet2Item = itemSet2

What is the big-O complexity of this algorithm?

这一生的挚爱 提交于 2020-01-14 15:48:08
问题 I have a function that I wrote below. This function is essentially a merge-sort. public static long nlgn(double[] nums) { if(nums.length > 1) { int elementsInA1 = nums.length/2; int elementsInA2 = nums.length - elementsInA1; double[] arr1 = new double[elementsInA1]; double[] arr2 = new double[elementsInA2]; for(int i = 0; i < elementsInA1; i++) arr1[i] = nums[i]; for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++) arr2[i - elementsInA1] = nums[i]; nlgn(arr1); nlgn(arr2); int i = 0

What is the big-O complexity of this algorithm?

夙愿已清 提交于 2020-01-14 15:47:52
问题 I have a function that I wrote below. This function is essentially a merge-sort. public static long nlgn(double[] nums) { if(nums.length > 1) { int elementsInA1 = nums.length/2; int elementsInA2 = nums.length - elementsInA1; double[] arr1 = new double[elementsInA1]; double[] arr2 = new double[elementsInA2]; for(int i = 0; i < elementsInA1; i++) arr1[i] = nums[i]; for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++) arr2[i - elementsInA1] = nums[i]; nlgn(arr1); nlgn(arr2); int i = 0

What is the big-O complexity of this algorithm?

和自甴很熟 提交于 2020-01-14 15:47:31
问题 I have a function that I wrote below. This function is essentially a merge-sort. public static long nlgn(double[] nums) { if(nums.length > 1) { int elementsInA1 = nums.length/2; int elementsInA2 = nums.length - elementsInA1; double[] arr1 = new double[elementsInA1]; double[] arr2 = new double[elementsInA2]; for(int i = 0; i < elementsInA1; i++) arr1[i] = nums[i]; for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++) arr2[i - elementsInA1] = nums[i]; nlgn(arr1); nlgn(arr2); int i = 0

What is the big-O complexity of this algorithm?

落花浮王杯 提交于 2020-01-14 15:45:34
问题 I have a function that I wrote below. This function is essentially a merge-sort. public static long nlgn(double[] nums) { if(nums.length > 1) { int elementsInA1 = nums.length/2; int elementsInA2 = nums.length - elementsInA1; double[] arr1 = new double[elementsInA1]; double[] arr2 = new double[elementsInA2]; for(int i = 0; i < elementsInA1; i++) arr1[i] = nums[i]; for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++) arr2[i - elementsInA1] = nums[i]; nlgn(arr1); nlgn(arr2); int i = 0