- 747
- 1
- 4
- 11
has a time complexity of O(n^2).
I think you are incorrectly using "n" as if it is the size of the n×ばつn matrix.
The "n" in O() notation refers to the total size of the input data.
So, for a ×ばつm matrix, the "n" value for complexity would be m2.
Your algorithm is O(n).
This code doesn't use extra memory
Is there a specific reason for doing the swap trick the way you did rather than simply using the more obvious method with an auto int temp;
on the stack?
auto
memory is allocated on a stack, so it exists only during the invocation of the function.
After that, it can be reused by other functions.
Unless a function is called recursively, there's no cumulative problem with using temporary stack space; that's what it's for.
And if an algorithm should ever actually require persistent storage, it would be done using static
rather than auto
.
This is C using the Original K&R C.
The biggest drawback to using that is that you don't get the benefit of typechecking, which was introduced in C89.
The compiler won't notice any problem with this:
swap(&_matrix[i][j],&_matrix[j][i]);
swap(_matrix[i][j],_matrix[j][i]);
swap("me", "you");
but at runtime things will fail.
I hope you are at least using lint
on the source to check for such problems.
has a time complexity of O(n^2).
I think you are incorrectly using "n" as if it is the size of the n×ばつn matrix.
The "n" in O() notation refers to the total size of the input data.
So, for a ×ばつm matrix, the "n" value for complexity would be m2.
Your algorithm is O(n).
This code doesn't use extra memory
Is there a specific reason for doing the swap trick the way you did rather than simply using the more obvious method with an auto int temp;
on the stack?
This is C using the Original K&R C.
The biggest drawback to using that is that you don't get the benefit of typechecking, which was introduced in C89.
The compiler won't notice any problem with this:
swap(&_matrix[i][j],&_matrix[j][i]);
swap(_matrix[i][j],_matrix[j][i]);
swap("me", "you");
but at runtime things will fail.
I hope you are at least using lint
on the source to check for such problems.
has a time complexity of O(n^2).
I think you are incorrectly using "n" as if it is the size of the n×ばつn matrix.
The "n" in O() notation refers to the total size of the input data.
So, for a ×ばつm matrix, the "n" value for complexity would be m2.
Your algorithm is O(n).
This code doesn't use extra memory
Is there a specific reason for doing the swap trick the way you did rather than simply using the more obvious method with an auto int temp;
on the stack?
auto
memory is allocated on a stack, so it exists only during the invocation of the function.
After that, it can be reused by other functions.
Unless a function is called recursively, there's no cumulative problem with using temporary stack space; that's what it's for.
And if an algorithm should ever actually require persistent storage, it would be done using static
rather than auto
.
This is C using the Original K&R C.
The biggest drawback to using that is that you don't get the benefit of typechecking, which was introduced in C89.
The compiler won't notice any problem with this:
swap(&_matrix[i][j],&_matrix[j][i]);
swap(_matrix[i][j],_matrix[j][i]);
swap("me", "you");
but at runtime things will fail.
I hope you are at least using lint
on the source to check for such problems.
has a time complexity of O(n^2).
I think you are incorrectly using "n" as if it is the size of the n×ばつn matrix.
The "n" in O() notation refers to the total size of the input data.
So, for a ×ばつm matrix, the "n" value for complexity would be m2.
Your algorithm is O(n).
This code doesn't use extra memory
Is there a specific reason for doing the swap trick the way you did rather than simply using the more obvious method with an auto int temp;
on the stack?
This is C using the Original K&R C.
The biggest drawback to using that is that you don't get the benefit of typechecking, which was introduced in C89.
The compiler won't notice any problem with this:
swap(&_matrix[i][j],&_matrix[j][i]);
swap(_matrix[i][j],_matrix[j][i]);
swap("me", "you");
but at runtime things will fail.
I hope you are at least using lint
on the source to check for such problems.