Ascending Order

Program to print numbers in ascending order

Page 1 of 1

3 Replies - 26758 Views - Last Post: 11 April 2007 - 02:07 AM Rate Topic: -----

#1 wavyaquaeyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-December 05

Ascending Order

Posted 01 April 2007 - 08:04 AM

HI Everyone,

I am new to java Programming. I am creating a program sp it prints 3 numbers in ascending order.

I know I need if statements and expressions(ie)


if (a > b && a > c)
print a
if (b > a && b >c)
print b
if (c > a && c >B)
print c
else if


[color=#FF0000]My problem is, I do not know what my next series of if expressions should be (I hope I am asking the question correctly_.

Can someone please help me?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Ascending Order

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Ascending Order

Posted 01 April 2007 - 08:27 AM

There are a few ways to do what you're looking for. You mention that you are looking for ascending order - the code above, assuming it represents the first set of instructions, would print the largest first, meaning that your numbers would appear in descending order. You are looking for the smallest first.

The most basic of ways to order the numbers is brute force - check the combinations. Assuming three variables named a,b,c, then you could do something like the following:
if(a<b&&a<c)
{
   System.out.println(a);
   if(b<c){
	  System.out.println(b);
	  System.out.println(c);
	  }
   else{
	  System.out.println(c);
	  System.out.println(b);
	  }
}
else if(b<a&&b<c)
{
   System.out.println(b);
   if(a<c){
	  System.out.println(a);
	  System.out.println(c);
	  }
   else{
	  System.out.println(c);
	  System.out.println(a);
	  }
}
else
{
   System.out.println(c);
   if(a<b){
	  System.out.println(a);
	  System.out.println(b);
	  }
   else{
	  System.out.println(b);
	  System.out.println(a);
	  }
}


Of course, the example assumes that no two values are equal.

I would advise, however, that it may be a better idea to put the values into a java Array object, and use the included sort() method. No point in reinventing the wheel! :)
Was This Post Helpful? 0
  • +
  • -

#3 stew_downunder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 07

Re: Ascending Order

Post icon  Posted 11 April 2007 - 01:58 AM

View PostAmadeus, on 1 Apr, 2007 - 08:27 AM, said:

I would advise, however, that it may be a better idea to put the values into a java Array object, and use the included sort() method. No point in reinventing the wheel! :)


I am only a novice too here, but I have learned so far that it is always preferable to use the "far less code" way of doing things, especially when it comes to sorting which as Amadeus pointed out is much easier with an array sorting method.

I have included my example here for Wavyaquaeyes - I hope this is of help.

import java.util.Arrays;

public class Wavyaquaeyes
{
	public static void main(String args[])
	{
		char characterArray[] = {'w', 'a', 'v', 'y', 'a', 'q', 'u', 'a', 'e', 'y', 'e', 's'};

		Arrays.sort(characterArray);

		for (int i = 0; i < characterArray.length; i++)
		{
		 	System.out.print(characterArray[i]);
		 	System.out.print(", ");
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 AngeluS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 231
  • Joined: 11-April 07

Re: Ascending Order

Posted 11 April 2007 - 02:07 AM

If you are trying to learn Java then you should write a sort function yourself instead of using the sort method that already exists.
For sorting and ordering you will always need an array or list or any other way to have items "grouped" together.

What you should do is placing all your numbers in an array.
Then you loop write your own bubble sort and you let that method wreak havoc on your array. That way you can get easily and (quite) fast a sorted list of numbers.
If you want to know about simple sorting then check: http://en.wikipedia....iki/Bubble_sort
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1