9 Replies - 7980 Views - Last Post: 06 May 2009 - 07:26 PM Rate Topic: -----

#1 tscott  Icon User is offline

  • D.I.C Regular

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

Sorting Array in Ascending order

Post icon  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  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sorting Array in Ascending order

Posted 05 May 2009 - 08:56 PM

View Posttnachbauer, on 5 May, 2009 - 06:46 PM, said:

How would I print the following array in ascending order?


Arrays.sort(array);
Was This Post Helpful? 0
  • +
  • -

#5 tscott  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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]
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sorting Array in Ascending order

Posted 05 May 2009 - 09:31 PM

View PostKYA, 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 :D 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
   }
}


Was This Post Helpful? 0
  • +
  • -

#8 tscott  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 tscott  Icon User is offline

  • D.I.C Regular

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

Re: Sorting Array in Ascending order

Posted 06 May 2009 - 01:21 PM

any help?>????
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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");
	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1