-4

What I need to do for my homework is to sort the digits of a positive number from low to high.

I can use

  • operators
  • integers/doubles/floats/chars
  • if/switch and loops

I cannot use

  • char array()
  • string
12
  • 3
    Welcome to Stack Overflow. Which language exactly? And what have you tried? You want to sort one number? o.O Commented Mar 28, 2014 at 18:51
  • 12
    Just one number? It's already sorted. Commented Mar 28, 2014 at 18:51
  • 2
    You said from low to high in the question, but your comment is high-to-low. Commented Mar 28, 2014 at 20:14
  • 4
    Bleh, why did this question get put on hold for being unclear 40 minutes after the question was edited to clarify? It's perfectly clear what the OP is asking now. Commented Mar 28, 2014 at 21:06
  • 2
    I stole this idea for a code golf question. Commented Mar 29, 2014 at 1:20

4 Answers 4

17

This will do it!

int number = 52146729;
int sortedNumber = 0;
for (int i = 9; i >= 0; i--)
{
    int tmpNumber = number;
    while (tmpNumber > 0)
    {
        int digit = tmpNumber % 10;             
        if (digit == i)
        {
            sortedNumber *= 10;
            sortedNumber += digit;
        }
        tmpNumber /= 10;                
    }               
}
System.out.println(sortedNumber);

This is java btw. Given the constraints this is pretty efficient, O(n).

Sign up to request clarification or add additional context in comments.

3 Comments

have you tried sorting number with 10 digits?
"It will work for arbitrarily long numbers up to Integer.MAX_VALUE". Not really, it won't work when (Integer.toString(number).length()!=Integer.toString(number).replaceAll("[3-9]", "").length())&&Integer.toString(number).length()==10 evaluates to true.
Then change the datatype to long.
0

Given the total lack of requirements, not sure this will help, but I would use a list and the LINQ OrderBy method if I was doing this in production code:

List<int> testList = new List<int> { 1, 5, 10, 4, 2 };
IEnumerable<int> orderedList = testList.OrderByDescending(x => x);

For sorting the digits, you have be very clear about what an "array" is. You'll need some sort of collection for this to ever work. I would use a list again:

List<int> digits = new List<int>();
int remainingNumber = 52146729;
while (remainingNumber > 0)
{
   digits.Add(remainingNumber % 10);
   remainingNumber /= 10;
}

IEnumerable<int> orderedDigits = testList.OrderByDescending(x => x);

This works because a x mod y returns the remainder of x/y. So 9 % 10 returns nine, so does 19 % 10, etc. Then you divide by 10 to get rid of one digit, and repeat until you run out. Then I use the same order function as before. This approach definitely doesn't use strings, or any explicit arrays (List is an array under the hood). Like I said, accomplishing this without any sort of collection is going to be really ugly.

Printing out that list would be easy (if you just need to print for your output):

foreach (int i in orderedDigits)
   Console.Write(i);

If you need an actual number back, thats going to be a bit harder, so I'll leave it off for now.

5 Comments

to be more precise: what i can actually use are any operators, integers/doubles/floats/chars(no char array()string)) if/switch and loops
Well, you are going to get into a big mess really quickly if you aren't allowed to use any of the C# collections. Doing the sort yourself wouldn't be too bad, just use a bubble sort since the collection is small. All sorts tend to work on some sort of collection though. Let me know what you think of my edit.
thanks for the answer but the problem is, i don't think i can is a list, because our teacher doesn't accept a solution using something we haven't learnt in class yet (like the list),to be more precise: what i can actually use are any operators, integers/doubles/floats/chars(no char array()string)) if/switch and loops
Are you allowed to use an array of ints? That would work as a replacement.
i can't, that's my problem
0

Why don't you try to use a tree?

Build a tree with two children: left and right. Left children store lower numbers and right children store higher numbers. Thus, you need to consider a Node having two possible choices. The first number would be the root of the tree. Next number can be stored in left or right.

Once the tree is full, you can access each number by using an in-order approach: 1- Read left; 2- Read parent; 3- Read rigth.

Check this out: http://en.wikipedia.org/wiki/Tree_traversal

7 Comments

can you give me an example of that?
note-what i can actually use are any operators, integers/doubles/floats/chars(no char array()string)) if/switch and loops
Add those limitations to the question itself, where people might see them without having to read every last comment on the page.
This is effectively a heap sort, a valid solution, though probably a bit complex for where it sounds like he is coming from :)
Yes, maybe is a little complex.
|
0

Do it radix style:

int count9
int count8
int count7
int count6
int count5
int count4
int count3
int count2
int count1
int count0

// loop over the number and fill in the counts. (either with characters or by using %10)

for (int i = 0; i < count9; i++)
    print "9"

// more for loops, etc, downto 8.

Definitely not production code, but that is pretty much ruled out by the constraints of the assignment.

Hope this helps!

2 Comments

well that's what i did but as you said it's not a production code, which is also a problem
It's impossible to write production code without the ability to use basic elements of the language, like characters, strings, dynamically sized lists & arrays. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.