Search code examples
javaarraysselection-sort

Using Selection sort within an array in Java


I have a class called Person, where I have a constructor, getters, and setters for three variables: name, age, and height. I also have implemented a Selection sort method in this class to sort the ages of people. Then I have created an array with ten people and I have given them different names, ages, and heights, but I have not gotten to use the Selection sort method to sort the ages of people. I would like to know if you could help me to know what I am doing wrong and why I have not gotten to use the method within my array.

I would also like to know if there is a smarter (less manual) way to implement an array of the type I would like to (with name, age, and height), because I will add more people, like 20 people, and that is going to demand some extra work that I guess I could save with some better method. I know how to do it with an array list, but I would like to know with an array if that is possible, or reasonable.

//Class Person

public class Person {
    private String name;
    private int height;
    private int age;

public void Person (String name, int height, int age){
    this.name = name;
    this.height = height;
    this.age = age;
}

public String getName (){
    return name;
}

public int getHeight (){
    return height;
}

public int getAge (){
    return age;
}

public void setName (String name) {
    this.name = name;
}

public void setHeight (int height) {
    this.height=height;
}

public void setAge (int age) {
    this.age=age;
}

public int[] selectionSort (int[] age){

    int i, j, minValue, minIndex, temp =0;

    for (i = 0; i<age.length; i++) {
        minValue = age[i];
        minIndex = i;

        for (j=i; j<age.length; j++) {

            if (age[i]<minValue){
                minValue = age [j];
                minIndex = j;
            }
        }
        if (minValue<age[i]){
            temp=age[i];
            age[i]=age[minIndex];
            age[minIndex]=temp;
        }
    }
return age;
}
}

//Array implementation

public class Main {

public static void main(String[] args) {

Person [] persons = new Person [3];

    persons [0] = new Person ();
    persons [0].setName("Josef");
    persons [0].setHeight(170);
    persons [0].setAge(30);

    persons [1] = new Person ();
    persons [1].setName("Marie");
    persons [1].setHeight(160);
    persons [1].setAge(35);

    persons [2] = new Person ();
    persons [2].setName("Karel");
    persons [2].setHeight(180);
    persons [2].setAge(40);

    for (int i=0; i<persons.length; i++){
        System.out.println("Jméno: " + persons[i].getName()+ ", věk: " + persons[i].getAge() + ", vyška: " + persons[i].getHeight());
    }

    //My main problem is here
    for (int i = 0; i<persons.length; i++){
        System.out.println(persons[i].selectionSort());
    }

}
}

Solution

  • There are several problems with your code, you have to study it online and clear your concepts. However, I am going to explain a bit:

    System.out.println(persons[i].selectionSort());
    

    Now, you have created this method selectionSort() and you can use this on an object of type Person but it is expecting a parameter of type int[], which you are not providing.

    It is a logical error, selectionSort doesn't work like that you can't call this method on every index of array. Its his job to sort the array at once. So, you have to pass the whole persons[] array and the rest will be done by selectionSort()

    public int[] selectionSort (int[] age)
    

    You are using int age[], you can't do this because you don't have an array of type int, what you have is array of type Person each object of type Person has an attribute age and you can access this by dot operator.

    Working Code:

    public class Person 
    {
        public static Person [] persons = new Person [3];  // so that every method can access this array
        private String name;
        private int height;
        private int age;  
    
        public void Person (String name, int height, int age){
            this.name = name;
            this.height = height;
            this.age = age;
        }
    
        public String getName (){
            return name;
        }
    
        public int getHeight (){
            return height;
        }
    
        public int getAge (){
            return age;
        }
    
        public void setName (String name) {
            this.name = name;
        }
    
        public void setHeight (int height) {
            this.height=height;
        }
    
        public void setAge (int age) {
            this.age=age;
        }
    
        public static void selectionSort(Person persons[])
        {
            int smallest;
    
            for(int i = 0; i < persons.length; i++)
            {
                smallest=i;
                for(int index = i+1; index<persons.length; index++)
                    if(persons[index].age<persons[smallest].age)
                        smallest=index;
    
                swap(i,smallest);   
            }
        }
    
        public static void swap(int frst, int scnd)
        {
            Person temporary = persons[frst];
            persons[frst] = persons[scnd];
            persons[scnd] = temporary;
        }
    
        public static void main(String[] args) 
        {
            persons [0] = new Person ();
            persons [0].setName("Josef");
            persons [0].setHeight(170);
            persons [0].setAge(35);
    
            persons [1] = new Person ();
            persons [1].setName("Marie");
            persons [1].setHeight(160);
            persons [1].setAge(31);
    
            persons [2] = new Person ();
            persons [2].setName("Karel");
            persons [2].setHeight(180);
            persons [2].setAge(40);
    
            for (int i=0; i<persons.length; i++){
                System.out.println("Jmeno: " + persons[i].getName()+ ", vek: " + persons[i].getAge() + ", vyska: " + persons[i].getHeight());
            }
    
            selectionSort(persons);
            for (int i = 0; i<persons.length; i++){
                System.out.println(persons[i].age);
            }
        }
    }
    

    NOTE: I have merged code in same class Person, but you can divide them into Main and Person always. It is recommended that if you divide the code then include selectionSort() in Main.