algorithm - What is the runtime complexity of this short code? -


i found solution on programming practice site , said complexity o(n). however, looks more o(n^2) me. can please tell me why o(n) ?

public static void transposematrix(int[][] matrix) {     int n = matrix.length - 1;     int temp = 0;     for(int = 0; <= n; i++){         for(int j = i+1; j <= n; j++){             temp = matrix[i][j];             matrix[i][j] = matrix[j][i];             matrix[j][i] = temp;         }     } } 

what n?

if n max(matrix.length, matrix[0].length), algorithm o(n^2), say.

if n the total size of matrix, algorithm o(n).

it's important define n in big-o notation. when learning big-o, discussions revolve around single-dimensional inputs, , people assume don't have define n. in real world, things dirty, deal multidimensional inputs, , have clear n is.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -