analysis

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

R cluster analysis and dendrogram with correlation matrix

℡╲_俬逩灬. 提交于 2020-01-21 09:21:50
问题 I have to perform a cluster analysis on a big amount of data. Since I have a lot of missing values I made a correlation matrix. corloads = cor(df1[,2:185], use = "pairwise.complete.obs") Now I have problems how to go on. I read a lot of articles and examples, but nothing really works for me. How can I find out how many clusters are good for me? I already tried this: dissimilarity = 1 - corloads distance = as.dist(dissimilarity) plot(hclust(distance), main="Dissimilarity = 1 - Correlation",

Difference between average case and amortized analysis

跟風遠走 提交于 2020-01-19 04:33:45
问题 I am reading an article on amortized analysis of algorithms. The following is a text snippet. Amortized analysis is similar to average-case analysis in that it is concerned with the cost averaged over a sequence of operations. However, average case analysis relies on probabilistic assumptions about the data structures and operations in order to compute an expected running time of an algorithm. Its applicability is therefore dependent on certain assumptions about the probability distribution

Time complexity analysis. choosing operator for counting number of times a line of code runs

旧街凉风 提交于 2020-01-11 11:47:43
问题 Analysing time complexity of this pseudocode. On the right my take on the number of times each line runs. I'm not sure whether to use log n , n log n , or simply n for the while-loop..please help times 1 sum = 0 1 2 i = 1 1 3 while i ≤ n log n + 1 4 for j = 1 to n n log n 5 sum = sum + j n log n 6 i = 2i log n 7 return sum 1 this results in: 2 n log + 2log n + 4 and thereby : O(n log n) is this correct ? 回答1: If your while loop is: 3 while i < n log n + 1 4 for j = 1 to n n log n 5 sum = sum

Finding the number of times a statement runs

拟墨画扇 提交于 2020-01-07 03:48:13
问题 I am told to figure out the number of times the statement foo runs in the following program. We are assuming that n is an even number. j = 1; while( j <= n/2 ) { i = 1; while( i <= j ) { foo; i++; } j++; } I figured the best way to go about this would be start from the inner loop and work my way outward. We know that i = 1 and inside the inner loop we have i <= j . That means this inner loop runs j times. (This is where I start to get confused) In the outer loop, we see the statement j <= n/2

Big O analysis for method with multiple parameters

拥有回忆 提交于 2020-01-06 14:38:14
问题 We're learning efficiency analysis in our intro to computer science class and I'm having trouble solving this problem. Suppose I have a method: public static void foo(int[][] arr, int num1, int num2) { for (int i=0;i<arr.length;i++) { arr[0][i] = num1*i; } for (int j=0;j<arr.length;j++) { arr[i][0] = num2*i } } My first question is if I have a method where there are 3 for-loops, but they are NOT nested, what would the growth rate be? Also, for this particular made-up method, would the input

Binary Counter Amortized Analysis

最后都变了- 提交于 2020-01-06 07:45:19
问题 I guess you already know that if all the entries in the Array starts at 0 and at each step we increment the counter by 1 (by flipping 0's and 1's) then the amortized cost for k increments is O(k). But, what happens if the Array starts with n ? I though that maybe the complexity for k increments is now O(log(n) + k), because of the fact that in the beginning the maximum number of 1's is log(n). Any suggestions ? Thanks in advance 回答1: You are right. There is more than one way to prove this,

Binary Counter Amortized Analysis

送分小仙女□ 提交于 2020-01-06 07:45:12
问题 I guess you already know that if all the entries in the Array starts at 0 and at each step we increment the counter by 1 (by flipping 0's and 1's) then the amortized cost for k increments is O(k). But, what happens if the Array starts with n ? I though that maybe the complexity for k increments is now O(log(n) + k), because of the fact that in the beginning the maximum number of 1's is log(n). Any suggestions ? Thanks in advance 回答1: You are right. There is more than one way to prove this,

“P/Invoke entry points should exist” with what should be correct entry points stated

有些话、适合烂在心里 提交于 2020-01-04 02:41:13
问题 I'm getting this warning from the Code Analysis tool in Visual Studio 2012. The code looks like this: using System; using System.Runtime.InteropServices; namespace MyProgramNamespace { class NativeMethods { [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")] public static extern IntPtr GetWindowLongPtr(IntPtr handle, int flag); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] public static extern IntPtr SetWindowLongPtr(IntPtr handle, int flag, IntPtr ownerHandle); } } I'm

Coq case analysis and rewrite with function returning subset types

混江龙づ霸主 提交于 2020-01-02 07:14:04
问题 I was working is this simple exercise about writing certified function using subset types. The idea is to first write a predecessor function pred : forall (n : {n : nat | n > 0}), {m : nat | S m = n.1}. and then using this definition give a funtion pred2 : forall (n : {n : nat | n > 1}), {m : nat | S (S m) = n.1}. I have no problem with the first one. Here is my code Program Definition pred (n : {n : nat | n > 0}) : {m : nat | S m = n.1} := match n with | O => _ | S n' => n' end. Next