Results 1 to 11 of 11
  1. #1
    flpanthers1 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default Sorting in ascending and descending order

    So I have to write code for 5 numbers in ascending order, and then descending order. I have the code below, but my results are not what I need. I thought I could just switch to the > sign in the code. Apparently not. Any ideas would be helpful

    Results:

    Array of numbers in ascending and descending order
    16 22 37 45 99


    Java Code:
    public class IntArray2 {
    	public static void main(String[] args) {
    	int i =0;
    	int[] list = {45,37,22,16,99};
    	for(i = list.length-1; i > 0; i--){
    		for(int k =0 ; k <list.length-1; k++ ){
    		int tempa = list[k];
    		int tempb = list[k+1];
    			if (list[k] > list[k+1]){
    		 	list[k] = tempb;
    		 	list[k+1] = tempa;
    			}
    			}
    			}
    	System.out.println("Array of numbers in ascending and descending order");
    		for(int x =0; x < list.length; x ++) 
            {
    		System.out.print(list[x] + " ");// Prints the numbers in ascending order
            }
       		for(int x =0; x > list.length; x ++) //Tried to switch to the > symbol thinking it would the print ascending order. Something is wrong here
            {
    		System.out.print(list[x] + " ");
    	}
            }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    results are not what I need.
    If you would post what the results are and comment them saying why they are wrong it would speed things up.

    Where is the code to sort the numbers in the other order?
    Last edited by Norm; 06-27-2011 at 04:06 AM.

  3. #3
    flpanthers1 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    The results from running the code is here...

    Array of numbers in ascending and descending order
    16 22 37 45 99


    the code for the descending numbers is here... This is the last part of the code. I thought I could just change the < to a >. Apparently this is not right.

    for(int x =0; x > list.length; x ++) //Tried to switch to the > symbol thinking it would the print ascending order. Something is wrong here
    {
    System.out.print(list[x] + " ");

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    for(int x =0; x > list.length; x ++)
    If the length of the list is 5 then how will it ever be possible for x (which is 0) to be greater? It will never enter this loop.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    What you are attempting to do is first sort the array and then print it in ascending and descending order. Is this correct? You might want to seek clarification as I believe you are supposed to sort in descending order as well.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    If you sort in ascending order and it works properly, write the opposite of the loop.

    for (int i=list.length; i > -1; i--){//Code}
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Quote Originally Posted by Dark View Post
    If you sort in ascending order and it works properly, write the opposite of the loop.

    for (int i=list.length; i > -1; i--){//Code}
    Which blows up with a AIOOBE on the first iteration...

    db

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    My apologies,
    for (int i=list.length-1; i > -1; i--){//Code}
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    flpanthers1 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    What you are attempting to do is first sort the array and then print it in ascending and descending order. Is this correct? You might want to seek clarification as I believe you are supposed to sort in descending order as well.
    Yes, this is correct. Ascending, and then into descending.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by flpanthers1 View Post
    Yes, this is correct. Ascending, and then into descending.
    You can leave your sorting algorithm as it is but look upon each number x in the array to be sorted as if it were -x, x < y <---> -x > -y

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    If you look at my second post, I put the loop you would use for your current sorting algorithm. You just reverse the loop you use to print it in ascending order.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 07:59 AM
  2. [SOLVED] Sorting array in descending order?
    By dan0 in forum New To Java
    Replies: 14
    Last Post: 04-16-2009, 12:19 AM
  3. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 04:31 AM
  4. Sorting in descending order
    By santanu in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 11:43 PM
  5. Descending order
    By santanu in forum New To Java
    Replies: 1
    Last Post: 11-04-2008, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •