why does this algorithm not work?
class Sort {
public static void main (String args[]) {
int a, b, c;
System.out.println("Geben Sie die erste Zahl ein:");
a = IO.readInt();
System.out.println("Geben Sie die zweite Zahl ein:");
b = IO.readInt();
System.out.println("Geben Sie die dritte Zahl ein:");
c = IO.readInt();
if (a < b & b< c) {
System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + b + ",
"+ c);
}else if (a< b & b>c ){
System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + c + ",
"+ b);
}else if (a>b & b>c ) {
System.out.println("Zahlen aufsteigend sortiert: "+ c +", " + b + ",
"+ a);
}else if (a>b & b<c) {
System.out.println("Zahlen aufsteigend sortiert: "+ b +", " + c + ",
"+ a);
}
}
}
Vova Reznik - 09 Feb 2006 22:26 GMT
> why does this algorithm not work?
> if (a < b & b< c) {
if (a < b && b < c)
Richard Wheeldon - 09 Feb 2006 22:52 GMT
> why does this algorithm not work?
> int a, b, c;
Imagine a=5, b=10, c=1
> if (a < b & b< c) {
> System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + b + ", "+ c);
b>c. Therefore this will not be executed
> }else if (a< b & b>c ){
a<b (5<10) and b>c (10>1)
Therefore this will.
> System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + c + ", "+ b);
And this is wrong. Also, you should use && not & as the previous poster
suggested,
Richard
Roedy Green - 10 Feb 2006 04:09 GMT
On Thu, 09 Feb 2006 22:52:10 +0000, Richard Wheeldon
<richard@rswheeldon.com> wrote, quoted or indirectly quoted someone
who said :
>And this is wrong. Also, you should use && not & as the previous poster
>suggested,
it is not wrong, just slower, and not always slower.
if a and b are booleans I think you will find
a & b executes faster than a && b
for the cases where a=true
& is a simple AND operation
&& is a test and condiditional jump in X86 which screws up the
pipelines if you take the jump.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 09 Feb 2006 22:55 GMT
> why does this algorithm not work?
> if (a < b & b< c) {
>
> System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + b + ",
> "+ c);
>
> }else if (a< b & b>c ){
Is a < c ???
> System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + c + ",
> "+ b);
[quoted text clipped - 3 lines]
> "+ a);
> }else if (a>b & b<c) {
Is a > c ???
> System.out.println("Zahlen aufsteigend sortiert: "+ b +", " + c + ",
> "+ a);
>
> }
In general, when using multiple inequalities, it is very much easier to
understand if you keep to < and <= rather than mixing > in their. So for
a simple range check:
if (min <= x && x <= max) { ... }
(&& is more conventional that &. It does much the same thing, only the
second condition is not evaluated if its not necessary. Also & has
another meaning to do with bit manipulation.)
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 10 Feb 2006 04:10 GMT
On Thu, 09 Feb 2006 23:15:40 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
> if (min <= x && x <= max) { ... }
This is the way the mathematicians have been doing it for hundreds of
years. I stick to that pattern.
At some point in future Java may let you abbreviate that:
if ( min <= x <= max)
the way the mathematicians do.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Stefan Ram - 10 Feb 2006 05:21 GMT
>At some point in future Java may let you abbreviate that:
>if ( min <= x <= max)
>the way the mathematicians do.
And Perl6ers IIRC.
In Java 6, there actually even is a special class for this
purpose:
»Returns true only when the test expression is between the
lower and upper bounds inclusive.«
http://www.docjar.com/docs/api/javax/management/BetweenQueryExp.html
But it does not seem to be intended for general use (it is not
public).
Tom Leylan - 10 Feb 2006 03:15 GMT
In addition to what other wrote let me suggest an "approach" to problem
solving. I call it "simplify, simplify, simplify" :-)
First it isn't actually "sorting" anything but let's say you can't get 3
numbers to work. Stop immediately and get 1 number to work, in other words
prompt for a number and print it out. Then have it print whether that
number is greater than or less than some constant. Got all that working,
add a second number and see if that works. You would have tried entering 5
as the first and second numbers and asked "hey what's it doing they're
equal"? You've made your debugging problem all the more difficult because
you introduced version three before version one and two worked.
Keep in mind as you optimize this that if you test whether a > b (or a >= b)
that you know the results of that and so it doesn't really have to be
checked again. It can safely be assumed that a is less than (or equal to) b
if that test doesn't pass therefore asking "if ( a > b )" once again
shouldn't be necessary.
Hope this helps,
Tom
"justaname" <hahn_thomas@gmx.at> wrote...
> why does this algorithm not work?
>
[quoted text clipped - 33 lines]
>
> }
blmblm@myrealbox.com - 10 Feb 2006 09:49 GMT
>why does this algorithm not work?
Others are giving you sensible advice, but here's another way to
think about whether it can work:
You're considering four possible cases. How many cases do you need
in order to consider all the possible arrangements of three numbers?
(Hint: There are more than four of them.)
A few more specific comments interspersed below.
>class Sort {
>
[quoted text clipped - 18 lines]
> System.out.println("Zahlen aufsteigend sortiert: "+ a +", " + c + ",
>"+ b);
How do you know here that a<c?
> }else if (a>b & b>c ) {
> System.out.println("Zahlen aufsteigend sortiert: "+ c +", " + b + ",
>"+ a);
> }else if (a>b & b<c) {
> System.out.println("Zahlen aufsteigend sortiert: "+ b +", " + c + ",
>"+ a);
How do you know here that c<a?
> }
>
> }
>
>
>}
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.