Блог пользователя MVernik

Автор MVernik, история, 4 года назад, По-английски

Confused...


Back-end part: int[][] matrix = new int[n][m]; int[] array = new int[n*m]; traversMatrix(matrix); traversArray(array); Client part: function traversMatrix(matrix : int[][]) for (i..n) for (j..m) doActions(); function traversArray(array : int[]) for (i..array.size()) doActions(); =================================== Time Complexity for traversMatrix: Quadratic Time Complexity for traversArray : Linear? Why?
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

"Quadratic" and "linear" are oversimplified, to tell the full story you have to say "quadratic in $$$x$$$" where $$$x$$$ is some variable.

For example, $$$t = 2^n$$$ is exponential in $$$n$$$, but it's actually sublinear in $$$4^n$$$. (If you substitute $$$m = 4^n$$$, you get $$$t = \sqrt{m}$$$.)

So your first method is quadratic in $$$n$$$ and $$$m$$$ (it's $$$t = n \times m$$$). Your second method is linear in $$$s$$$ (if you let $$$s$$$ be the length of the array), since it's $$$t = s$$$, but $$$s = n \times m$$$ so this is just $$$t = n \times m$$$ again. They take the same runtime.