# Sorting Array in Ascending order

Page 1 of 1

## 9 Replies - 11058 Views - Last Post: 06 May 2009 - 07:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=103578&amp;s=903c26f6ad80a705ed619258f5aa13e6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tscott

• D.I.C Regular

Reputation: 2
• Posts: 370
• Joined: 30-January 09

# Sorting Array in Ascending order

Posted 05 May 2009 - 07:46 PM

How would I print the following array in ascending order?

```
import java.util.Random;
public class Project6
{
public static void main (String [] args)
{
Hero();

}

public static void Hero ()
{
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
for(int i = 0; i < array.length; i++)
{for(int j= i+1; j <array.length; j++)
{array[i] = RNum.nextInt(1000);
System.out.println(array[i]);
if (array[i] >= 500)
count++;
if (array[i] == array[j])
System.out.print("The following numbers were duplicates" +j);
}
}

System.out.println("The array had " + count + " elements over 500");

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Sorting Array in Ascending order

### #2 tscott

• D.I.C Regular

Reputation: 2
• Posts: 370
• Joined: 30-January 09

## Re: Sorting Array in Ascending order

Posted 05 May 2009 - 08:51 PM

My book talks about making one a min value and one a max value in a nested loop but it doesn't make much sense to me in the problem I have mainly because I don't know where to put it. Will I make that statement outside my first loop brackets?

### #3 KYA

• Wubba lubba dub dub!

Reputation: 3180
• Posts: 19,210
• Joined: 14-September 07

## Re: Sorting Array in Ascending order

Posted 05 May 2009 - 08:53 PM

sorted ascending order or ascending based on index values?

### #4 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Sorting Array in Ascending order

Posted 05 May 2009 - 08:56 PM

tnachbauer, on 5 May, 2009 - 06:46 PM, said:

How would I print the following array in ascending order?

Arrays.sort(array);

### #5 tscott

• D.I.C Regular

Reputation: 2
• Posts: 370
• Joined: 30-January 09

## Re: Sorting Array in Ascending order

Posted 05 May 2009 - 09:20 PM

sort the ascending based on index values

This is what I have and it doesn't work I get an error

```
import java.util.Random;
public class Project6
{
public static void main (String [] args)
{
Hero();

}

public static void Hero ()
{
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
for(int i = 0; i < array.length; i++)
{for(int j= i+1; j <array.length; j++)
{array[i] = RNum.nextInt(1000);
System.out.println(array[i]);
if (array[i] >= 500)
count++;

}
}

System.out.println(Arrays.sort(array);

System.out.println("The array had " + count + " elements over 500");

}
}

```

1 error found:
File: C:\Documents and Settings\Tim\Desktop\javaprograms\Projects\Project6.java [line: 32]
Error: C:\Documents and Settings\Tim\Desktop\javaprograms\Projects\Project6.java:32: ')' expected

This post has been edited by tnachbauer: 05 May 2009 - 09:23 PM

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3180
• Posts: 19,210
• Joined: 14-September 07

## Re: Sorting Array in Ascending order

Posted 05 May 2009 - 09:21 PM

That's paradoxical. The array is already ordered in sequential index values. If you need it sorted, see pbl's post above and then iterate starting at [0] going to [size-1]

### #7 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Sorting Array in Ascending order

Posted 05 May 2009 - 09:31 PM

KYA, on 5 May, 2009 - 08:21 PM, said:

That's paradoxical. The array is already ordered in sequential index values. If you need it sorted, see pbl's post above and then iterate starting at [0] going to [size-1]

Good point... at least now I know that you read my topics That's a friend

It was in another post I think

```int[] array = new int[1000];
int[] nb = new int[1000];
for(int i = 0; i < 1000; i++) {
array[i] = rand.nextInt(1000);
nb[array[i]]++;
}

```

So now
nb[0] = number of 0 in array
nb[1] = number of 1 in array
....
nb[999] = number of 999 in array

it is almost as sorted. To create a "real" sorted array 2 choices:

1) Arrays.sort(array);

2)
```int k = 0;
int[] sorted = new int[1000];	   // new sorted array
for(int i = 0; i < 1000; i++) {	  // pass through all number 0 to 999
for(j = 0; j < nb[i]; j++) {	   // number I had of that number
sorted[k++] = i;				  // store it in new slot in sorted array
}
}

```

### #8 tscott

• D.I.C Regular

Reputation: 2
• Posts: 370
• Joined: 30-January 09

## Re: Sorting Array in Ascending order

Posted 06 May 2009 - 11:54 AM

Ok so now I want to print array[i] outside the loop correct? But whenever I do that it tells me it can't find variable i? so what would I be printing?

```import java.util.Random;
public class Project6
{
public static void main (String [] args)
{
Hero();

}

public static void Hero ()
{
int k = 0;
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
int[] nb = new int[1000];
for(int i = 0; i < array.length; i++)
{array[i] = RNum.nextInt(1000);
nb[array[i]]++;
System.out.println(array[i]);
if (array[i] >= 500)
count++;

}
int[] sorted = new int[1000];
for(int i = 0; i < 1000; i++)
{for(int j = 0; j < nb[i]; j++)
{ sorted[k++] = i;
}
}
System.out.println("The array had " + count + " elements over 500");
}

}

```

This post has been edited by tnachbauer: 06 May 2009 - 11:56 AM

### #9 tscott

• D.I.C Regular

Reputation: 2
• Posts: 370
• Joined: 30-January 09

## Re: Sorting Array in Ascending order

Posted 06 May 2009 - 01:21 PM

any help?>????

### #10 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Sorting Array in Ascending order

Posted 06 May 2009 - 07:26 PM

I am impressed
Random() does a very good job when you look at the output

```import java.util.Random;
public class project6
{
public static void main (String [] args)
{
Hero();

}

public static void Hero ()
{
int k = 0;
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
int[] nb = new int[1000];
for(int i = 0; i < array.length; i++) {
array[i] = RNum.nextInt(1000);
nb[array[i]]++;
System.out.println(array[i]);
if (array[i] >= 500)
count++;

}
int[] sorted = new int[1000];
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < nb[i]; j++) {
System.out.println("[" + k + "] = " + i);
sorted[k++] = i;
}
}
System.out.println("The array had " + count + " elements over 500");
}

}

```