How to sort array by divisor sum faster? I am sorting an array by divisor sum in ascending order. If two or more numbers have the same sum of divisors they have to be sorted in ascending order. The problem I have is that my version is not fast enough. Here is my code:

int divisorSum( double number )
{
    double sum = number + 1;
    for( int i = 2; i &lt;= number / 2; i++ )
        if(( int )number % i == 0 )
            sum += i;
    return sum;
}
void sortByDivisorSum( double *array, int length )
{
    for( int i = 0; i &lt; length; i++ )
    {
        for( int j = i + 1; j &lt; length; j++ )
        {
            if(( divisorSum( array[ i ]) &gt; divisorSum( array[ j ])) || (( divisorSum( array[ i ] ) == divisorSum( array[ j ])) &amp;&amp; ( array[ i ] &gt; array[ j ])))
            {
                array[ i ] += array[ j ];
                array[ j ] = array[ i ] - array[ j ];
                array[ i ] -= array[ j ];
            }
        }
    }
}
void readArrayy(double *array, int length)
{
    for(int index = 0; index &lt; length; index++)
        scanf("%lf", &amp;array[index]);
}
int main( void )
{
    int length, in;
    scanf( "%d", &amp;length );
    double ar[ length ];
    readArrayy( ar, length );
    sortByDivisorSum( ar, length );
    for( in = 0; in &lt; length; in++ )
    {
        printf( "%.0lf ", ar[in]);
    }
    return 0;
}


How to solve this in a faster way?

Input:

10
24 46 11 36 48 35 27 28 49 6


Output:

6 11 27 35 28 49 24 46 36 48
Many elements of this code can be much faster.

Calculating the sum of divisors will be faster if you:


Iterate until sqrt(number) instead of number / 2, and include divisor pairs (i and number / i)
Change the type of number from double to int


(See also my review of your related question.)

Use a faster sorting algorithm, for example quicksort or merge sort.
And when you, avoid mistakes like if(( divisorSum( array[ i ]) &gt; divisorSum( array[ j ])) || (( divisorSum( array[ i ] ) == divisorSum( array[ j ])) &amp;&amp; ( array[ i ] &gt; array[ j ]))), which recomputes the sum of divisors twice for array[i] and array[j].
You could store the results of expensive operations in a variable,
and then you can use that variable multiple times in conditions.