943,871 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 40562
  • Java RSS
Nov 8th, 2003
0

Selection Sort in java

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dina is offline Offline
12 posts
since Nov 2003
Nov 9th, 2003
0

Re: Selection Sort in java

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
Reputation Points: 47
Solved Threads: 1
Newbie Poster
oberoc is offline Offline
20 posts
since Nov 2003
Mar 29th, 2006
-1

Re: Selection Sort in java

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
psodhi is offline Offline
11 posts
since Mar 2006
Mar 29th, 2006
0

Re: Selection Sort in java

Quote 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!!!!
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2006
0

Re: Selection Sort in java

hii
try to use two function rather tha two class
Reputation Points: 10
Solved Threads: 0
Newbie Poster
malaya_champ is offline Offline
2 posts
since Apr 2006
Nov 6th, 2009
-1

Selection sort with arrays using methods

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]);
}
}
Reputation Points: 8
Solved Threads: 0
Newbie Poster
elitehaxor7 is offline Offline
1 posts
since Nov 2009
Jan 21st, 2010
0
Re: Selection Sort in java
hai..pwedi nyo b akong bgyan ng isang code about selection sort....thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jhanylyn is offline Offline
1 posts
since Jan 2010
Jan 21st, 2010
0
Re: Selection Sort in java
Click to Expand / Collapse  Quote originally posted by Jhanylyn ...
hai..pwedi nyo b akong bgyan ng isang code about selection sort....thanks
Start a new thread and write proper english
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Sep 4th, 2010
-1

A selection sort with a user input

can you gave us a running program about selection sort that the user will input something from the keyboard...thank you...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hazelmayt@yahoo is offline Offline
1 posts
since Sep 2010
Sep 4th, 2010
1
Re: Selection Sort in java
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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: How to delete a component, when it is double-clicked?
Next Thread in Java Forum Timeline: Insert row in Jtable at run time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC