Selection Sort in java

Reply

Join Date: Nov 2003
Posts: 12
Reputation: dina is an unknown quantity at this point 
Solved Threads: 0
dina dina is offline Offline
Newbie Poster

Selection Sort in java

 
1
  #1
Nov 8th, 2003
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 20
Reputation: oberoc is on a distinguished road 
Solved Threads: 1
oberoc oberoc is offline Offline
Newbie Poster

Re: Selection Sort in java

 
0
  #2
Nov 9th, 2003
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 11
Reputation: psodhi is an unknown quantity at this point 
Solved Threads: 1
psodhi psodhi is offline Offline
Newbie Poster

Re: Selection Sort in java

 
0
  #3
Mar 29th, 2006
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<x.length;i++) //loop for putting the values into the array
x[i]=Integer.parseInt(arg[i]);

selectionSort(x); //calling the method for sorting,defined later in the class

//printing the sorted list
System.out.println("Sorted List:-");

for(i=0;i<x.length;i++)
System.out.println(x[i]);
}
catch(NumberFormatException E) //traps the error if specified arg is not a number
{
System.out.println("Given argument is not a number...\n");
}
}
}

static void selectionSort(int x[]) //method for sorting the list,made static for using in main
{
int i,j;
int pos=0; //variable to fix a position in array
int min=0; //variable to fix a number as minimum.

for(i=0;i<x.length-1;i++)
{
min=x[i+1]; //setting the minimum number
pos=i+1; //fixing the position to the index of that number

for(j=i+1;j<x.length;j++)
{
if(min>x[j]) //if number at x[j] is graeter than minimum then
{
min=x[j]; //put x[j] in min
pos=j; //and change position to index number of j
}
}

if(x[pos]<x[i]) //if number at x[pos] is less than number at x[i] then swap the values
{
x[pos]=x[pos]+x[i];
x[i]=x[pos]-x[i];
x[pos]=x[pos]-x[i];
}
}
}
}


hope this code will help you..
we do not need to create to separate classes for sorting a list here.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Selection Sort in java

 
0
  #4
Mar 29th, 2006
Originally Posted by psodhi
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<x.length;i++) //loop for putting the values into the array
x[i]=Integer.parseInt(arg[i]);

selectionSort(x); //calling the method for sorting,defined later in the class

//printing the sorted list
System.out.println("Sorted List:-");

for(i=0;i<x.length;i++)
System.out.println(x[i]);
}
catch(NumberFormatException E) //traps the error if specified arg is not a number
{
System.out.println("Given argument is not a number...\n");
}
}
}

static void selectionSort(int x[]) //method for sorting the list,made static for using in main
{
int i,j;
int pos=0; //variable to fix a position in array
int min=0; //variable to fix a number as minimum.

for(i=0;i<x.length-1;i++)
{
min=x[i+1]; //setting the minimum number
pos=i+1; //fixing the position to the index of that number

for(j=i+1;j<x.length;j++)
{
if(min>x[j]) //if number at x[j] is graeter than minimum then
{
min=x[j]; //put x[j] in min
pos=j; //and change position to index number of j
}
}

if(x[pos]<x[i]) //if number at x[pos] is less than number at x[i] then swap the values
{
x[pos]=x[pos]+x[i];
x[i]=x[pos]-x[i];
x[pos]=x[pos]-x[i];
}
}
}
}


hope this code will help you..
we do not need to create to separate classes for sorting a list here.
NICE WORK!!! You did his homework for him!!!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 2
Reputation: malaya_champ is an unknown quantity at this point 
Solved Threads: 0
malaya_champ malaya_champ is offline Offline
Newbie Poster

Re: Selection Sort in java

 
0
  #5
Apr 1st, 2006
hii
try to use two function rather tha two class
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: elitehaxor7 is an unknown quantity at this point 
Solved Threads: 0
elitehaxor7 elitehaxor7 is offline Offline
Newbie Poster

Selection sort with arrays using methods

 
-1
  #6
17 Days Ago
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]);
}
}
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC