Rotate square matrix clockwise by 90degrees #include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;malloc.h&gt;

void nl(void) {     //function to print new line
    printf("\n");
}

int** allocate(_size) {
    int** matrix=(int**)malloc(sizeof(int*)*_size);
    for(int i=0;i&lt;_size;i++) {
        matrix[i]=(int*)malloc(sizeof(int)*_size);
    }
    return matrix;
}

void scanMatrix(_matrix,_size)
int** _matrix;
{
    printf("Enter the values: ");
    for(int i=0;i&lt;_size;i++) {
        for(int j=0;j&lt;_size;j++) {
            scanf("%d",&amp;_matrix[i][j]);
        }
    }
}

void printMatrix(_matrix,_size)
int** _matrix;
{
    for(int i=0;i&lt;_size;i++) {
        for(int j=0;j&lt;_size;j++) {
            printf("%d ",_matrix[i][j]);
        }
        nl();
    }
}

void swap(num1,num2)
int* num1;
int* num2;
{
    *num1=*num1+*num2;
    *num2=*num1-*num2;
    *num1=*num1-*num2;
}

void rotateMatrix(_matrix,_size)
int** _matrix;
{
    for(int i=0;i&lt;_size;i++) {
        for(int j=i+1;j&lt;_size;j++) {
            swap(&amp;_matrix[i][j],&amp;_matrix[j][i]);
        }
    }
    for(int i=0;i&lt;_size;i++) {
        for(int j=0;j&lt;_size/2;j++) {
            swap(&amp;_matrix[i][j],&amp;_matrix[i][_size-1-j]);
        }
    }
}

main(argc,argv)
const char** argv;
{
    int size=0;
    printf("Enter the size of the square matrix: ");
    scanf("%d",&amp;size);
    int** matrix=allocate(size);
    scanMatrix(matrix,size);
    printMatrix(matrix,size);
    nl();
    rotateMatrix(matrix,size);
    nl();
    printMatrix(matrix,size);
    return 0;
}


This code doesn't use extra memory and also uses run-time allocation using malloc. However, has a time complexity of O(n^2). How do I further optimise it?
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×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&amp;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(&amp;_matrix[i][j],&amp;_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.