954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Selection Sort in java

Hi everyone,

Just need abit of help. im studying selection sorting at the mo.....ive wrote an essay wot it and how it works but im tryin to an example in java and im having problems with that.....im not a very good java user.....ive only just started.

i just wanted to know do u or would it be best to use 2 differnt classes - one for the sorting and one to enter the numbers u want sorting?

also if u were 2 use 2 differnt classes how would u connect the 2 together?

Thanks

dina
Newbie Poster
12 posts since Nov 2003
Reputation Points: 11
Solved Threads: 0
 

Actually, you could probably use


public static void main (String[] main) {

Initialize your class
Write a loop to get your numbers
send to method in that classs

}

So, your answer is. You don't have to use two classes. In fact, just one for your selection sort and have the user input numbers in the public static void part.

-Tino

oberoc
Newbie Poster
20 posts since Nov 2003
Reputation Points: 47
Solved Threads: 1
 

hi..
I am writing the code for selection sort, you'll need to pass the numbers for sorting as command line arguments that is along with the command you use to run the java program

e.g., java Selection 12,45,1,76,50

the code is as follows:

class Selection
{
public static void main(String arg[])
{
int x[]; //declaring an empty array,initialized later
int i;

//checking if arguments have been given or not at the command line
if(arg.length==0)
System.out.println("No Arguments Specified...\n");//prints msg if no args specified

else
{
x=new int[arg.lenght]; //initializing the array with the no. of args specified.

try //trapping any errors that might occur
{
for(i=0;i

psodhi
Newbie Poster
11 posts since Mar 2006
Reputation Points: 10
Solved Threads: 1
 

hi.. I am writing the code for selection sort, you'll need to pass the numbers for sorting as command line arguments that is along with the command you use to run the java program

e.g., java Selection 12,45,1,76,50

the code is as follows:

class Selection { public static void main(String arg[]) { int x[]; //declaring an empty array,initialized later int i;

//checking if arguments have been given or not at the command line if(arg.length==0) System.out.println("No Arguments Specified...\n");//prints msg if no args specified

else { x=new int[arg.lenght]; //initializing the array with the no. of args specified. try //trapping any errors that might occur { for(i=0;i

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

hii
try to use two function rather tha two class

malaya_champ
Newbie Poster
2 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

import java.util.*;
public class DS2
{
public static void main(String[] args)
{
Scanner k=new Scanner(System.in);
int[] ds=new int[10];
for (int z=0;z<10;z++)
{
Random R=new Random();
int x=R.nextInt(999)+1;
ds[z]=x;
}
System.out.println("Numbers Before Sort:\n"+ds[1]+","+ds[2]+","+ds[3]+","+ds[4]+","+ds[5]+","+ds[6]+","+ds[7]+","+ds[8]+","+ds[9]+"\n");
Arrays.sort(ds);
System.out.print("Numbers After Sort:\n"+ds[1]+","+ds[2]+","+ds[3]+","+ds[4]+","+ds[5]+","+ds[6]+","+ds[7]+","+ds[8]+","+ds[9]);
}
}

elitehaxor7
Newbie Poster
1 post since Nov 2009
Reputation Points: 8
Solved Threads: 0
 

hai..pwedi nyo b akong bgyan ng isang code about selection sort....thanks

Jhanylyn
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
hai..pwedi nyo b akong bgyan ng isang code about selection sort....thanks

Start a new thread and write proper english

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

can you gave us a running program about selection sort that the user will input something from the keyboard...thank you...

hazelmayt@yahoo
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
can you gave us a running program about selection sort that the user will input something from the keyboard...thank you...

NO.

Start a new thread. There are plenty of examples on how to read input with the Scanner class. Start a new thread with some code.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You