Results 1 to 13 of 13

Thread: Sorting help

  1. #1
    clerd is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Sorting help

    I'm trying to write a program that generates 100 random number, prints them, then sorts them, then prints out the sorted list with ten numbers in each line. I cannot figure out what is wrong with my code. Anyone have any ideas? A detailed explanation would be awesome as I am new to this. This is what I have so far.

    Java Code:
    public class Main {
    
    	public static void main(String[]Args)
    	{
    		int [] b;
    		b = new int[100];
    		loadArray(b);
    		printArray(b);
    		Sort(b); 
    		printArray(b);
    	}
    
    	public static void loadArray(int[] b) {
    		for(int i = 0; i < b.length; i++){
    			b[i]= (int)((Math.random())*100)+1;{
    				
    			}
    		}
    		
    	}
    	
    	public static void printArray(int[] b){
    		System.out.println();
    		for(int i = 0; i < b.length; i++){
    			System.out.print(b[i]);
    			if((i + 1)%10 == 0)
    				System.out.println();
    		}
    	}
    	
    	public static void Sort(int [] b)
    	{
    		int Temp = 0;
    		for (int i = 0; i < b.length-1; i++)
    		{
    			for (int j = i + 1; j < b.length; j++)
    			{
    				if (b[i]> b[j])
    					{
    					Temp = b[i];
    					b[i] = b[j];
    					b[j] = Temp;
    					}
    			}
    			}
    			
    		}
    	
    
    }
    And here is an example of what it prints out right now.

    42629035334048469325
    21775561975248917456
    9340882010067304331
    5542393117952289996
    3775521286392295798
    83166760847191818556
    696337363376546310037
    99637451574393191
    54155896825250881413
    4479358294746523

    1244455688
    9111213141516202123
    23252829293031313333
    35363737373739394040
    42434546474848505252
    52525454545556565757
    58586061626363676769
    71747475767779818283
    84858688909191919393
    9393969696979899100100

    As you can see all of the lines have more than 10 numbers in them except for the first line of the sorted list, which is also the only line that is sorted. Any help would be appreciated, thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Sorting help

    Add some spaces in between the numbers you're printing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Sorting help

    Crossposted: Sorting question
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    clerd is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Re: Sorting help

    Quote Originally Posted by KevinWorkman View Post
    Add some spaces in between the numbers you're printing.
    What do you mean? Wouldn't there still be more than 100 numbers in each list?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Sorting help

    Quote Originally Posted by clerd View Post
    What do you mean? Wouldn't there still be more than 100 numbers in each list?
    You print this out:

    9393969696979899100100

    Instead, print this out:

    93 93 96 96 96 97 98 99 100 100
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    clerd is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Re: Sorting help

    Quote Originally Posted by KevinWorkman View Post
    You print this out:

    9393969696979899100100

    Instead, print this out:

    93 93 96 96 96 97 98 99 100 100
    Omg derp haha thanks

  7. #7
    clerd is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Re: Sorting help

    Quote Originally Posted by KevinWorkman View Post
    You print this out:

    9393969696979899100100

    Instead, print this out:

    93 93 96 96 96 97 98 99 100 100
    I can't think of any way to do that :[

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Sorting help

    No way at all? What have you tried? You know how to print out a single number. Why can't you print out a single space?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Sorting help

    Quote Originally Posted by KevinWorkman View Post
    Crossposted: Sorting question
    Another: Sorting question (Beginning Java forum at JavaRanch)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Sorting help

    Aaand I'm done. Turns out I wasted my time giving you help you already received. This is exactly why crossposting is considered so rude.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Sorting help

    Actually, my understanding is that cross posting is fine as long as the OP let's you know.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Sorting help

    Quote Originally Posted by jim829 View Post
    Actually, my understanding is that cross posting is fine as long as the OP let's you know.

    Regards,
    Jim
    Exactly. The OP did *not* let us know, even when I asked him to link between crossposts to avoid exactly what happened. It's not a big deal, but it does get annoying since I mostly only answer questions while my code compiles!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Sorting help

    Quote Originally Posted by KevinWorkman View Post
    It's not a big deal, but it does get annoying since I mostly only answer questions while my code compiles!
    Ha! I answer questions when my code doesn't compile. Guess why I spam so much!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Sorting by Last Name
    By burrish in forum New To Java
    Replies: 4
    Last Post: 10-15-2011, 05:39 PM
  2. Sorting
    By amzers in forum NetBeans
    Replies: 1
    Last Post: 10-04-2011, 06:06 PM
  3. Sorting
    By Freakzoyd in forum New To Java
    Replies: 3
    Last Post: 01-21-2011, 02:02 AM
  4. little help with sorting
    By drgnfire25 in forum New To Java
    Replies: 3
    Last Post: 01-15-2011, 09:23 PM
  5. Help in sorting
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 06:15 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
  •