5 Replies - 4113 Views - Last Post: 13 May 2009 - 04:31 AM Rate Topic: -----

#1 PA88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 09-March 09

Help with sorting numbers in an array

Post icon  Posted 13 May 2009 - 12:07 AM

Iv made a program that generates numbers random numbers like in a Super Lotto game. However I am having trouble sorting them so that they are displayed from lowest to highest. I've attempted to do so through the use of a for loop. If anyone could take a look at my code please let me know what I'm doing wrong here because when I run the program I still dont get the numbers in order from lowest to highest.

Also it should be noted that I dont want the last number (mega number) in order, this one will always be placed last.

import java.util.Scanner;
import java.util.Random;

public class SuperLotto
{
	private Scanner input = new Scanner( System.in );
	private Random randomNumbers = new Random();
	
	public int selectTickets()
	{
		int tickets = input.nextInt();
		return tickets;
	} //end seletTickets
	
	public void generateNumbers( int tickets, int[] array)
	{
		for( int ticket = 0; ticket < tickets; ticket++ )
		{
			System.out.print( "\n****************\n" );
			for( int lottonumbers = 0; lottonumbers < 5; lottonumbers++ )
			{
				array[lottonumbers] = 1 + randomNumbers.nextInt(47);

			} //end inner for
			for( int numbers = 0; numbers < 5; numbers++ )
			{
				int temp;
				if ( array[numbers] > array[numbers + 1] )
				{
					temp = array[numbers];
					array[numbers] = array [numbers + 1];
					array[numbers + 1] = temp;
				}
				System.out.printf( "%d ", array[numbers]);
			}
			


			int mega = 1 + randomNumbers.nextInt(27);
			System.out.printf( "%d ", mega );
			array[5] = mega;
		} //end outer for
		System.out.print( "\n****************\n" );
	} //end generateNumbers
	
	public static void main( String args[] )
	{
		int tickets = 1;
		int array[] = new int[6];
		 
		SuperLotto game = new SuperLotto();
		System.out.print( "Welcome to SuperLotto\nEnter desired amount of tickets: " );
		tickets = game.selectTickets();
		game.generateNumbers( tickets, array );
		System.out.print( "Thank You For Playing\n" );
	} //end main
} //end public SuperLotto


Is This A Good Question/Topic? 0
  • +

Replies To: Help with sorting numbers in an array

#2 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Help with sorting numbers in an array

Posted 13 May 2009 - 12:24 AM

is it compulsory to implement your own comparison method or i guess you can use the comparator interface and then use the Arrays.sort(array) function of java

This post has been edited by prajayshetty: 13 May 2009 - 12:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 PA88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 09-March 09

Re: Help with sorting numbers in an array

Posted 13 May 2009 - 01:29 AM

Ah I didn't know about this method. I think I was suppose to use a my own method but thanks for the info.
Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Help with sorting numbers in an array

Posted 13 May 2009 - 01:46 AM

actually Arrays.sort does the sorting of the elments in an array for you normally they do by sorting by default they do by ascending order but you can change it by implement comparator or compaarable interface refer sun site for java doc
Was This Post Helpful? 0
  • +
  • -

#5 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Help with sorting numbers in an array

Posted 13 May 2009 - 02:42 AM

import java.util.Arrays;
import java.util.Collections;
 
public class SortArrayWithOrder {
	public static void main(String[] args) {
		Integer[] points = new Integer[5];
		points[0] = 94;
		points[1] = 53;
		points[2] = 70;
		points[3] = 44;
		points[4] = 64;
 
		//
		// Sort the points array, the default order is in ascending order.
		// [44, 53, 64, 70, 94]
		//
		Arrays.sort(points);
		System.out.println(Arrays.toString(points));
 
		//
		// Sort the points array in descending order.
		// [94, 70, 64, 53, 44]
		//
		Arrays.sort(points, Collections.reverseOrder());
		System.out.println(Arrays.toString(points));
	}
}



just check it out hope it works
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Help with sorting numbers in an array

Posted 13 May 2009 - 04:31 AM

This

			for( int numbers = 0; numbers < 5; numbers++ )
			{
				int temp;
				if ( array[numbers] > array[numbers + 1] )
				{
					temp = array[numbers];
					array[numbers] = array [numbers + 1];
					array[numbers + 1] = temp;
				}
				System.out.printf( "%d ", array[numbers]);
			}


has to be done as long as a swap occured
if you have 5 4 3 2
your program will swap 5 an 4
the new 5 and 3
the new 5 and 2
and you'll end up with 4 3 2 5
You'll never go back to swap the 4 and the 3
try
do {
		 boolean swapOccured = false;
		 for( int numbers = 0; numbers < 5; numbers++ )
		 {
			  int temp;
			  if ( array[numbers] > array[numbers + 1] )
			  {
					temp = array[numbers];
					array[numbers] = array [numbers + 1];
					array[numbers + 1] = temp;
					swapOccured = true;
			   }
			}
 } while(swapOccured);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1