473,466 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Selection sort help.....

22 New Member
HI,
I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a compareTo method which will compare two person objects by the birthday. If the birthday are the same it outputs int 0, if one is bigger it outputs int 1 and if the other one is bigger it outputs int -1. I have also created an arraylist called people, which holds objects of person. Now i am trying to create a selection sort method which will sort the objects into birthday order, here is the code i have so far but am really stumped on what to do now...



import java.util.ArrayList.*;


public class Main
{

public static void selectionSort(Comparable[] people)
{


for (int k = people.length-1; k>0; k --)
{
Comparable tmp;
int Large = 0;

for (int j=1; j<= k; j++)
{
if (people[j].compareTo(people[k]) <0)
{
Large = j;
}
tmp = people[Large];
people[Large] = people[k];
people[k] = tmp;


}
}
}
}

Any help would be brilliant,

Thanks a lot

Dave
May 2 '07 #1
12 2550
JosAH
11,448 Recognized Expert MVP
What's the problem? The selection sort itself? There are numerous implementations
available on Google.

kind regards,

Jos
May 2 '07 #2
ShaveDave27
22 New Member
What's the problem? The selection sort itself? There are numerous implementations
available on Google.

kind regards,

Jos
The main problem is getting the birthdays of person and using selection sort to sort them. I have the selection sort code which compiles but i cant make it look into the array people and sort the objects by birthday.

I'll have a look on google but any more help would be excellent.

Thanks

Dave
May 2 '07 #3
r035198x
13,262 MVP
The main problem is getting the birthdays of person and using selection sort to sort them. I have the selection sort code which compiles but i cant make it look into the array people and sort the objects by birthday.

I'll have a look on google but any more help would be excellent.

Thanks

Dave
Tips on sorting.
May 2 '07 #4
JosAH
11,448 Recognized Expert MVP
The main problem is getting the birthdays of person and using selection sort to sort them. I have the selection sort code which compiles but i cant make it look into the array people and sort the objects by birthday.

I'll have a look on google but any more help would be excellent.

Thanks

Dave
Your Person class implements the Comparable interface right? If so there's
nothing more to do. Persons can compare themselves against (other) Persons
and they use their birthdays as the comparison criteria, right?

Your sort method shouldn't and couldn't care less about *what* it's sorting
as long as those things can compare themselves. The sort method shouldn't
even care about birthdays, first- or last-names. To be honest with you: I
don't really understand your problem: you can compare birthdays (the Person
class can do it itself because if implements the Comparable interface).
Just let your sort method sort those Comparables.

kind regards,

Jos
May 2 '07 #5
ShaveDave27
22 New Member
Your Person class implements the Comparable interface right? If so there's
nothing more to do. Persons can compare themselves against (other) Persons
and they use their birthdays as the comparison criteria, right?

Your sort method shouldn't and couldn't care less about *what* it's sorting
as long as those things can compare themselves. The sort method shouldn't
even care about birthdays, first- or last-names. To be honest with you: I
don't really understand your problem: you can compare birthdays (the Person
class can do it itself because if implements the Comparable interface).
Just let your sort method sort those Comparables.

kind regards,

Jos

Yeah i understand that now, but i the problem is getting the class to sort between an array of person objects. i have just posted another string with my code on. I need it to sort the array which i cant get it to do. I understand that the person class will compare two person objects but i cant take the output of either -1,0 or 1 and put it into the sorting algorithm and use that to sort the array. any ideas?
thank Jos you've been a lot of help.
May 2 '07 #6
JosAH
11,448 Recognized Expert MVP
Yeah i understand that now, but i the problem is getting the class to sort between an array of person objects. i have just posted another string with my code on. I need it to sort the array which i cant get it to do. I understand that the person class will compare two person objects but i cant take the output of either -1,0 or 1 and put it into the sorting algorithm and use that to sort the array. any ideas?
thank Jos you've been a lot of help.
You're trying to put too much information into too little sentences ;-)

1) your Person class implements the Comparable interface, so two Persons
can compare themselves, i.e. I can do:
Expand|Select|Wrap|Line Numbers
  1. int result= Jeff.compareTo(Beth);
2) You have a Comparable array full of Persons:
Expand|Select|Wrap|Line Numbers
  1. Comparable[] comps= { 
  2.    new Person("Jeff", "Jones", ...), 
  3.    new Person("Beth", "Scott", ...)
  4. };
3) What is the problem? Are you unable to implement your sorting method
in terms of Comparable things?

kind regards,

Jos
May 2 '07 #7
ShaveDave27
22 New Member
You're trying to put too much information into too little sentences ;-)

1) your Person class implements the Comparable interface, so two Persons
can compare themselves, i.e. I can do:
Expand|Select|Wrap|Line Numbers
  1. int result= Jeff.compareTo(Beth);
2) You have a Comparable array full of Persons:
Expand|Select|Wrap|Line Numbers
  1. Comparable[] comps= { 
  2.    new Person("Jeff", "Jones", ...), 
  3.    new Person("Beth", "Scott", ...)
  4. };
3) What is the problem? Are you unable to implement your sorting method
in terms of Comparable things?

kind regards,

Jos

Yes,
1) i cant implement my sorting method to my comparable things.
2) Also I need to make this sorting method in a new class and I cant find out how to call my array into the new class.
3) My sorting method has to look at every person in the array and compare it with the one next to it to find out whether it is greater or not.

Thanks Jos
May 2 '07 #8
JosAH
11,448 Recognized Expert MVP
Yes,
1) i cant implement my sorting method to my comparable things.
2) Also I need to make this sorting method in a new class and I cant find out how to call my array into the new class.
3) My sorting method has to look at every person in the array and compare it with the one next to it to find out whether it is greater or not.

Thanks Jos
You're welcome. I guess that point 2) is the major hurdle, right? Why not
just pass that array in as a parameter and return the same array again as
a convenience. You can even make your sorter class a utility class, i.e. just
static methods. Read my "tip of the week" article (one of the older ones) and
see how it's done. Here's an outline:
Expand|Select|Wrap|Line Numbers
  1. public class SelectionSort {
  2.    private SelectionSort() { } // no instantiations of this class possible
  3.    //
  4.    public static Comparable[] sort(Comparable[] a) {
  5.       // a whole lot of voodoo in here, i.e. your sorting method
  6.       return a;
  7.    }
  8. }
Now you can sort any array like this:
Expand|Select|Wrap|Line Numbers
  1. Comparable[] a = { ... };
  2. Comparable[] sortedArray= SelectionSort.sort(a);
kind regards,

Jos
May 2 '07 #9
ShaveDave27
22 New Member
You're welcome. I guess that point 2) is the major hurdle, right? Why not
just pass that array in as a parameter and return the same array again as
a convenience. You can even make your sorter class a utility class, i.e. just
static methods. Read my "tip of the week" article (one of the older ones) and
see how it's done. Here's an outline:
Expand|Select|Wrap|Line Numbers
  1. public class SelectionSort {
  2.    private SelectionSort() { } // no instantiations of this class possible
  3.    //
  4.    public static Comparable[] sort(Comparable[] a) {
  5.       // a whole lot of voodoo in here, i.e. your sorting method
  6.       return a;
  7.    }
  8. }
Now you can sort any array like this:
Expand|Select|Wrap|Line Numbers
  1. Comparable[] a = { ... };
  2. Comparable[] sortedArray= SelectionSort.sort(a);
kind regards,

Jos

Hi,

I've done what you've said and it's looking very good.
One last thing - in the space where i call my array i have entered People.people.
This is because my array is in the People class and it is named people.
But the method cant find it.
Do i enter the [] somewhere?
or am i being very stupid!

Thankyou again for all your help,

Dave
May 2 '07 #10
Ganon11
3,652 Recognized Expert Specialist
It is probably complaining because the array people is private in the People class - as it should be. Instead, refer to the parameter name - in Jos' example, a. Use a instead of People.people.
May 2 '07 #11
JosAH
11,448 Recognized Expert MVP
Hi,

I've done what you've said and it's looking very good.
One last thing - in the space where i call my array i have entered People.people.
This is because my array is in the People class and it is named people.
But the method cant find it.
Do i enter the [] somewhere?
or am i being very stupid!

Thankyou again for all your help,

Dave
What is the type of that "people" array? Did you do this:
Expand|Select|Wrap|Line Numbers
  1. Person[] people;
or:
Expand|Select|Wrap|Line Numbers
  1. Comparable[] people;
kind regards,

Jos
May 2 '07 #12
ShaveDave27
22 New Member
What is the type of that "people" array? Did you do this:
Expand|Select|Wrap|Line Numbers
  1. Person[] people;
or:
Expand|Select|Wrap|Line Numbers
  1. Comparable[] people;
kind regards,

Jos
Ok here is my code for my people class:

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2. import java.util.ArrayList.*;
  3.  
  4. public class People
  5. {
  6.  
  7. public int ArrayList (String[] args)
  8. {
  9. Person Chris = new Person("Chris", "Davies", 10, 7);
  10. Person Jess = new Person ("Jess", "Levy", 4, 17);
  11. Person Dave = new Person ("Dave", "McDonald", 4, 14);
  12. Person Andy = new Person ("Andy", "Davies", 3, 6);
  13.  
  14.  
  15. ArrayList<Person> people = new ArrayList<Person>();
  16. people.add(Chris);
  17. people.add(Jess );
  18. people.add(Dave );
  19. people.add( Andy );
  20.  
  21.  
  22. int i = people.size();
  23.  
  24.  
  25. System.out.println("Size = " + i);
  26. Person first = people.get(0);
  27. System.out.println(first.GetFirst_name()+"'s Birthday = "+ first.GetDay()+ "/"+ first.GetMonth());
  28. Person Second = people.get(1);
  29. System.out.println(Second.GetFirst_name()+"'s Birthday = "+ Second.GetDay()+ "/"+ Second.GetMonth());
  30. Person Third = people.get(2);
  31. System.out.println(Third.GetFirst_name()+"'s Birthday = "+ Third.GetDay()+ "/"+Third.GetMonth());
  32. Person Fourth = people.get(3);
  33. System.out.println(Fourth.GetFirst_name()+"'s Birthday = "+ Fourth.GetDay()+ "/"+ Fourth.GetMonth());
  34.  
  35. return i;
  36. }}
  37.  
  38.  
  39. and here is the code for my main class (sorting method):
  40.  
  41.  
  42. import java.util.Arrays.*;
  43.  
  44. public class main
  45. {
  46.  
  47.  
  48. public static Comparable[] sort(Comparable [] a)
  49. {
  50. int j;
  51. for (int i = 0; i<a.length; i++)
  52. {
  53. Comparable value = a[i];
  54. for ( j = i; j>0; j--);
  55. {
  56. if (a[j-1].compareTo (a) <0)
  57. {
  58. break;
  59. }
  60. else
  61. {
  62. a[j] = a[j-1];
  63. }
  64. }
  65. a[j] = value;
  66.  
  67.  
  68. }
  69. return a;
  70. }
  71.  
  72. public int sortArray()
  73. {
  74. Comparable[] a = {People[] .people};
  75. Comparable[] sortedArray = main.sort(a);
  76. }
  77. }
  78.  
I've changed the People[].people part to everything i can think of but nothiong works. Thanks
Dave
May 2 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: srampally | last post by:
I need the capabilty to hide/show a selection list, just the way its done at http://www.lufthansa.com (place the cursor over "Group Companies"). However, I am looking for a javascript that is much...
4
by: shaveta | last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
1
by: gemacjr1201 | last post by:
I need hints for an algorithm using string compare in a selection sort. I will be sorting lastnames and first namesalphbetically. if the selection sort encounters the same last name it then goes to...
1
by: ShaveDave27 | last post by:
Hi, I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday). Now...
0
Ganon11
by: Ganon11 | last post by:
Earlier, mmccarthy was kind enough to post a short article explaining the Bubble Sort. As she said, this is a relatively slow sorting algorithm. Here, I will attempt to present a slightly faster...
4
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
1
mfshake
by: mfshake | last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
1
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.