| | |
Selection Sort in java
![]() |
•
•
Join Date: Nov 2003
Posts: 12
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2003
Posts: 20
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Mar 2006
Posts: 11
Reputation:
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<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.
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.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
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.
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
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]);
}
}
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]);
}
}
![]() |
Similar Threads
- Selection Sort Method + Comparison (Java)
- Problem converting Selection Sort to Class (C++)
- Selection Sort and String (C++)
- Some help needed with Selection Sort in java, Please!! (Java)
Other Threads in the Java Forum
- Previous Thread: Host's Non-Local IP Address
- Next Thread: Incompatible types found (HELP!)
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






