2

I found an algorithm which sorts the digits of a number to its smallest form. IE 54321 ==> 12345.

The algorithm looks like:

int main()
{
    int x = 0;
    cin >> x;

    for ( int l = 0; l < 10; ++l )
    {
        int rem = x % 10;
        int tx = x / 10;
        while ( rem || tx )
        {
            if ( rem == l ) cout << rem;
            rem = tx % 10;
            tx = tx / 10;
        }
    }
    cout << endl;
}

Could somebody explain to me what the big oh notation of this function would be? I'm not sure if it makes a difference on the size of the number (I am using numbers 9/10 digits long).

I have to develop two algorithms to check if one number contains all the digits of another number. One method I used brute force so I just had a double for loop checking to see if digits from number one existed from number two (numbers are represented as strings).

The second algorithm has to be more efficient so I am trying to sort the numbers using the algorithm above and just compare using a single for loop after. I thought this would take less time but it is taking far more time than the double for loop brute method and i'm not sure why.

Raphael
  • 73,212
  • 30
  • 182
  • 400
FreeStyle4
  • 21
  • 1

0 Answers0