-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
kojiro
  • 74,557
  • 19
  • 143
  • 201
user3473908
  • 21
  • 1
  • 1
  • 2
  • 3
    Welcome to Stack Overflow. Which language exactly? And what have you tried? You want to sort one number? o.O – Soner Gönül Mar 28 '14 at 18:51
  • 12
    Just one number? It's already sorted. – Mike B Mar 28 '14 at 18:51
  • What are the *actual* requirements? (An array is simply one implementation of an indexable sequence..) – user2864740 Mar 28 '14 at 18:51
  • Without the help of strings? Well... this should be easy... – Moo-Juice Mar 28 '14 at 18:53
  • Well what i mean is i need to sort a number, let's say: 52146729 – user3473908 Mar 28 '14 at 18:56
  • it's supposed to be 97654221, now the problem is how can i do this without using arrays or strings (This is what makes this homework hard for me) – user3473908 Mar 28 '14 at 18:57
  • And what specific programming language? – David Thomas Mar 28 '14 at 19:06
  • 2
    You said *from low to high* in the question, but your comment is high-to-low. – kojiro Mar 28 '14 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. – Mike B Mar 28 '14 at 21:06
  • 2
    I stole this idea for a [code golf question](http://codegolf.stackexchange.com/questions/25003/sort-a-number-sorta). – kojiro Mar 29 '14 at 01:20
  • Please add a tag indicating what language you're using. – Mark Reed Mar 29 '14 at 14:11
  • 2
    @Mike: because **a:** we don't know what language he's using (or is limited to), **b:** he's added limitations/restrictions in comments to posted answers and not added those to the question, implying that **c:** that there may well be *other* restrictions he's not bothered to tell us about and **d:** has shown no attempt at a solution, or explained the problems in the attempted solution(s). The last two aren't relevant to 'unclear' but they played a large part in *my* decision to close. – David Thomas Mar 30 '14 at 11:46

4 Answers4

16

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).

Mike B
  • 5,390
  • 2
  • 23
  • 45
  • have you tried sorting number with 10 digits? – Mc Kevin Mar 31 '14 at 04:18
  • "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. – Mc Kevin Apr 01 '14 at 02:52
  • Then change the datatype to `long`. – Mike B Apr 01 '14 at 14:09
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.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • to be more precise: what i can actually use are any operators, integers/doubles/floats/chars(no char array()string)) if/switch and loops – user3473908 Mar 28 '14 at 18:58
  • 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. – BradleyDotNET Mar 28 '14 at 19:06
  • 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 – user3473908 Mar 28 '14 at 19:07
  • Are you allowed to use an array of ints? That would work as a replacement. – BradleyDotNET Mar 28 '14 at 19:09
  • i can't, that's my problem – user3473908 Mar 28 '14 at 19:14
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

andresram1
  • 131
  • 5
  • can you give me an example of that? – user3473908 Mar 28 '14 at 19:02
  • note-what i can actually use are any operators, integers/doubles/floats/chars(no char array()string)) if/switch and loops – user3473908 Mar 28 '14 at 19:03
  • 1
    Add those limitations to the question itself, where people might see them without having to read every last comment on the page. – David Thomas Mar 28 '14 at 19:08
  • This is effectively a heap sort, a valid solution, though probably a bit complex for where it sounds like he is coming from :) – BradleyDotNET Mar 28 '14 at 19:13
  • Yes, maybe is a little complex. – andresram1 Mar 28 '14 at 19:14
  • can you give me an example? in C or C#, and thanks for the help ;) – user3473908 Mar 28 '14 at 19:18
  • Maybe this code is good for you: http://www.engineersgarage.com/c-language-programs/perform-tree-operations-insert-traversal-preorderpost-order-and-order Just focus in the insert function to build your tree (Maybe you need a loop). And then focus in inorder function to plot your sorted numbers. – andresram1 Mar 28 '14 at 19:21
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!

Barett
  • 5,826
  • 6
  • 51
  • 55
  • well that's what i did but as you said it's not a production code, which is also a problem – user3473908 Mar 28 '14 at 19:19
  • 2
    It's impossible to write production code without the ability to use basic elements of the language, like characters, strings, dynamically sized lists & arrays. ;) – Barett Mar 28 '14 at 19:22