Closed Thread Icon

Topic awaiting preservation: Must be simple but, . . . (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12115" title="Pages that link to Topic awaiting preservation: Must be simple but, . . . (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Must be simple but, . . . <span class="small">(Page 1 of 1)</span>\

 
Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 03-16-2002 03:48

I hate books that have exericises but no answers.

Here is the question (Java):

Write a method printSorted(int a, int b, int c) that prints its three inputs in sorted order.

To this point of the book, arrays haven't been mentioned yet.

How to I sort this three numbers?

Thx.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 03-16-2002 07:14

Sorting is a complex topic. There are many ways to sort things.

Since the book doesn't mention arrays you might look at it as six simple cases. With the posibilities being.

abc
acb
bac
bca
cab
cba

Thinking along these lines you could do 6 if statements. One for each case.

If you need to do this with an array then you will need to move the values using a temp integer.

I think there must be a better way to do this. But it is not on the top of my head right now.

I think this might be the best way, for what you are trying to do with 3 numbers.

If you were to have 4 numbers then you would need to use an array and do a bubble sort or some other sort method.

If you could further clarify the problem, and I am given more time to think on this I might get a better solution.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 03-16-2002 12:31

uh, you gotta implement an sorting algorithm for arrays. there are hundreds of variations out there.
search google for sorting arrays.


*is reminded of last years algorithm exam*
bubblesort
quicksort
rootsort
insertionsort
selectionsort
shellsort
mergesort
bucketsort
...

Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 03-16-2002 14:28

WarMage, that's what I thought but it looked very inefficient to me.

This question comes in the CH7 and the arrays are covered in the CH11, so they ask for an another way and I guess the one that you showed, may be the only one.

I'll take a look at the array solution too, thanks guys.

Sasha.


[This message has been edited by Sash (edited 03-16-2002).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 03-17-2002 08:04

If I hadn't been taught arrays yet, I'd probably do something like this...

if (b < a) {
temp = b;
b = a;
a = temp;
}
// a and b are now in correct order
if (c < a) {
temp = c;
c = b;
b = a;
a = temp;
}
else if (c < b) {
temp = c;
c = b;
b = temp;
}
// everything's in order, go ahead and print 'em.

« BackwardsOnwards »

Show Forum Drop Down Menu