2 Replies - 742 Views - Last Post: 28 August 2013 - 10:43 AM Rate Topic: -----

#1 kadrek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-March 13

Sorting Help

Posted 28 August 2013 - 09:21 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Help

#2 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 136
  • Joined: 19-July 13

Re: Sorting Help

Posted 28 August 2013 - 10:34 AM

Well, for the printing I would just do:
for(int i = 0; i < b.length; i++)
{
   if(i % 10 = 0)
     System.out.println();
   System.out.print(b[i]);
}


And for the sorting can't you just do
Arrays.sort(B)/>;

Btw, your sort code will only sort 2 numbers in turn (i.e if your last 2 numbers are 5,6 then they will stay there, but they have no need to go to the front). I'm not entirely sure how you would sort a number array besides counting how many counts of each number there are and then re-entering them into the array starting from 0 (or 1).

EDIT: Can anyone explain why the "/>" keeps appearing in the code? It capitalized the "b" in my second code example and put those two symbols after the closing parenthesis...

This post has been edited by StrongJoshua: 28 August 2013 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Sorting Help

Posted 28 August 2013 - 10:43 AM

in your printArray method use

System.out.printf("%4d", b[i]);

instead of System.out.print(b[i]);

and please avoid to use a "b" as a variable name it screws up with smileys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1