Selection sort help.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShaveDave27
    New Member
    • Apr 2007
    • 22

    #1

    Selection sort help.....

    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.Array List.*;


    public class Main
    {

    public static void selectionSort(C omparable[] 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(peop le[k]) <0)
    {
    Large = j;
    }
    tmp = people[Large];
    people[Large] = people[k];
    people[k] = tmp;


    }
    }
    }
    }

    Any help would be brilliant,

    Thanks a lot

    Dave
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What's the problem? The selection sort itself? There are numerous implementations
    available on Google.

    kind regards,

    Jos

    Comment

    • ShaveDave27
      New Member
      • Apr 2007
      • 22

      #3
      Originally posted by JosAH
      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by ShaveDave27
        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.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by ShaveDave27
          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

          Comment

          • ShaveDave27
            New Member
            • Apr 2007
            • 22

            #6
            Originally posted by JosAH
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by ShaveDave27
              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:
              Code:
              int result= Jeff.compareTo(Beth);
              2) You have a Comparable array full of Persons:
              Code:
              Comparable[] comps= { 
                 new Person("Jeff", "Jones", ...), 
                 new Person("Beth", "Scott", ...)
              };
              3) What is the problem? Are you unable to implement your sorting method
              in terms of Comparable things?

              kind regards,

              Jos

              Comment

              • ShaveDave27
                New Member
                • Apr 2007
                • 22

                #8
                Originally posted by JosAH
                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:
                Code:
                int result= Jeff.compareTo(Beth);
                2) You have a Comparable array full of Persons:
                Code:
                Comparable[] comps= { 
                   new Person("Jeff", "Jones", ...), 
                   new Person("Beth", "Scott", ...)
                };
                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

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by ShaveDave27
                  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:
                  Code:
                  public class SelectionSort {
                     private SelectionSort() { } // no instantiations of this class possible
                     //
                     public static Comparable[] sort(Comparable[] a) {
                        // a whole lot of voodoo in here, i.e. your sorting method
                        return a;
                     }
                  }
                  Now you can sort any array like this:
                  Code:
                  Comparable[] a = { ... };
                  Comparable[] sortedArray= SelectionSort.sort(a);
                  kind regards,

                  Jos

                  Comment

                  • ShaveDave27
                    New Member
                    • Apr 2007
                    • 22

                    #10
                    Originally posted by JosAH
                    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:
                    Code:
                    public class SelectionSort {
                       private SelectionSort() { } // no instantiations of this class possible
                       //
                       public static Comparable[] sort(Comparable[] a) {
                          // a whole lot of voodoo in here, i.e. your sorting method
                          return a;
                       }
                    }
                    Now you can sort any array like this:
                    Code:
                    Comparable[] a = { ... };
                    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

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      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.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by ShaveDave27
                        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:
                        Code:
                        Person[] people;
                        or:
                        Code:
                        Comparable[] people;
                        kind regards,

                        Jos

                        Comment

                        • ShaveDave27
                          New Member
                          • Apr 2007
                          • 22

                          #13
                          Originally posted by JosAH
                          What is the type of that "people" array? Did you do this:
                          Code:
                          Person[] people;
                          or:
                          Code:
                          Comparable[] people;
                          kind regards,

                          Jos
                          Ok here is my code for my people class:

                          Code:
                           import java.util.*; 
                          import java.util.ArrayList.*;
                           
                          public class People
                          {
                           
                          public int ArrayList (String[] args)
                          {
                          Person Chris = new Person("Chris", "Davies", 10, 7);
                          Person Jess = new Person ("Jess", "Levy", 4, 17);
                          Person Dave = new Person ("Dave", "McDonald", 4, 14);
                          Person Andy = new Person ("Andy", "Davies", 3, 6);
                           
                           
                          ArrayList<Person> people = new ArrayList<Person>();
                          people.add(Chris);
                          people.add(Jess );
                          people.add(Dave );
                          people.add( Andy );
                           
                           
                          int i = people.size();
                           
                           
                          System.out.println("Size = " + i);
                          Person first = people.get(0);
                          System.out.println(first.GetFirst_name()+"'s Birthday = "+ first.GetDay()+ "/"+ first.GetMonth());
                          Person Second = people.get(1);
                          System.out.println(Second.GetFirst_name()+"'s Birthday = "+ Second.GetDay()+ "/"+ Second.GetMonth());
                          Person Third = people.get(2);
                          System.out.println(Third.GetFirst_name()+"'s Birthday = "+ Third.GetDay()+ "/"+Third.GetMonth());
                          Person Fourth = people.get(3);
                          System.out.println(Fourth.GetFirst_name()+"'s Birthday = "+ Fourth.GetDay()+ "/"+ Fourth.GetMonth());
                           
                          return i;
                          }}
                           
                           
                          and here is the code for my main class (sorting method):
                           
                           
                          import java.util.Arrays.*;
                           
                          public class main
                          {
                           
                           
                          public static Comparable[] sort(Comparable [] a)
                          {
                          int j;
                          for (int i = 0; i<a.length; i++)
                          {
                          Comparable value = a[i];
                          for ( j = i; j>0; j--);
                          {
                          if (a[j-1].compareTo (a) <0)
                          {
                          break;
                          }
                          else
                          {
                          a[j] = a[j-1];
                          }
                          }
                          a[j] = value;
                           
                           
                          }
                          return a;
                          }
                           
                          public int sortArray()
                          {
                          Comparable[] a = {People[] .people};
                          Comparable[] sortedArray = main.sort(a);
                          }
                          }
                          I've changed the People[].people part to everything i can think of but nothiong works. Thanks
                          Dave

                          Comment

                          Working...