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.

  • 2
    $\begingroup$ Welcome to CS.SE! We get asked this kind of question a lot, so we put together some reference material that is relevant, especially cs.stackexchange.com/q/192/755, cs.stackexchange.com/q/23593/755. You should be able to solve this problem using the techniques described ther. Can you take a look over that, try again to solve your problem, and then if you're still stuck, edit your question to show your attempt and at what stage you got stuck? $\endgroup$
    – D.W.
    Mar 11, 2017 at 0:03
  • $\begingroup$ Welcome to Computer Science! The title you have chosen is not well suited to representing your question. Please take some time to improve it; we have collected some advice here. Thank you! $\endgroup$
    – Raphael
    Mar 11, 2017 at 10:02
  • 3
    $\begingroup$ Possible duplicate of Is there a system behind the magic of algorithm analysis? $\endgroup$ Mar 11, 2017 at 15:06


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.