# Sorting question

• August 28th, 2013, 11:17 AM
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.

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.
• August 28th, 2013, 11:30 AM
KevinWorkman
Re: Sorting question
This thread has been cross posted here:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

• August 28th, 2013, 11:34 AM
Re: Sorting question
Quote:

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

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.
• August 28th, 2013, 11:38 AM
KevinWorkman
Re: Sorting question
Quote:

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

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.
• August 28th, 2013, 12:21 PM
Re: Sorting question
Quote:

Originally Posted by KevinWorkman

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.
• August 28th, 2013, 02:04 PM
syedbhai
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.
• August 28th, 2013, 08:16 PM
Junky
Re: Sorting question
Quote: