Program to display array elements in the ascending order of number of factors each element has Input:

1000 23 100 26 32


Output:

23  26  32  100 1000


Since 23 has '2' factors and 26 has '4' factors and 32 has '6' factors, and so on. 


Should I use a linked list for these types of problems?
Is my code efficient? Or how can it be optimized?


Code:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int facts(int);

int main(void) {
    int n,a[2][25],i,j;
    printf("Enter the no. of items\n");
    scanf("%d",&amp;n);
    printf("Enter the items\n");
    for(j=0; j&lt;n; j++)
    {
        scanf("%d",&amp;a[0][j]);
    }
    for(j=0; j&lt;n; j++)
    {
        a[1][j] = facts(a[i][j]);
    }

    for(int x=0; x&lt;n; x++)
    {
        for(int y=0; y&lt;n-1; y++)
        {
            if(a[1][y]&gt;a[1][y+1])
            {
                int temp = a[1][y+1];
                int temp1 = a[0][y+1];
                a[1][y+1] = a[1][y];
                a[0][y+1] = a[0][y];
                a[1][y] = temp;
                a[0][y] = temp1;
            }
        }
    }

    for(j=0; j&lt;n; j++)
        {
            printf("%d\t", a[0][j]);
        }

    return 0;
}

int facts(int h)
{
    int factors=0;
    for(int k=1; k&lt;=h; k++)
    {
        if((h%k)==0)
        {
            factors++;
        }
    }
    return factors;
}
int main(void) {
    int n,a[2][25],i,j;
    printf("Enter the no. of items\n");
    scanf("%d",&amp;n);
    printf("Enter the items\n");
    for(j=0; j&lt;n; j++)
    {
        scanf("%d",&amp;a[0][j]);
    }
    for(j=0; j&lt;n; j++)
    {
        a[1][j] = facts(a[i][j]);         // here i is not set yet
    }


Your compiler should've warned you about this. Please make sure all variables have a set value before checking them.

The only reasons this works is because your compiler decided to initialize i at zero here. However, it is not required to do so by the specification. This means it will break on other systems.