Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Sorting question

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting question

    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.

    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sorting question

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/81219-sorting-help.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting question

    Quote Originally Posted by KevinWorkman View Post
    This thread has been cross posted here:

    http://www.java-forums.org/new-java/81219-sorting-help.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    I know, I'm sorry. I usually can figure it out faster if I get more advice though.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sorting question

    Quote Originally Posted by kadrek View Post
    I know, I'm sorry. I usually can figure it out faster if I get more advice though.
    The least you can do is provide links between crossposts, that way we know what other help you've already received. There's no point in answering your question if you've already figured it out on another forum.

    It might be faster for you, but it's a huge waste of our time, which is pretty rude considering we're doing this in our spare time for free.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting question

    Quote Originally Posted by KevinWorkman View Post
    The least you can do is provide links between crossposts, that way we know what other help you've already received. There's no point in answering your question if you've already figured it out on another forum.

    It might be faster for you, but it's a huge waste of our time, which is pretty rude considering we're doing this in our spare time for free.
    I'm sorry, I'll be more courteous next time.

    I've found out that the list is sorted, there are just no spaces between the double digit numbers. I can't think of any way to add them.

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Sorting question

    Hello.
    You are not printing the numbers properly. How can someone like us know whether a number is 1-digit or 2-digit? Even though you are printing ten numbers in a row try to put some space in between every two numbers.
    Then we can say if the numbers are really sorted or not. May be its sorted but we can't differentiate because of them being adjacent to each other.
    If the problem is still not solved let us know.

    Syed.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Sorting question

    Quote Originally Posted by kadrek View Post
    I can't think of any way to add them.
    Really?

    You can write the code to print a number but you have no idea how to print a space after that number.
    Improving the world one idiot at a time!

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sorting question

    He already received this advice on two other forums. Seems he got the answer and is long gone.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Sorting an ArrayList
    By gatorsgirl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2012, 01:22 PM
  2. Question about writing a phonebook sorting program
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: March 25th, 2011, 09:25 AM
  3. [ask] about sorting number.
    By bontet in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2010, 02:07 PM
  4. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  5. Sorting Algorithms
    By Dalisra in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM