Home > OS >  How to use selection sort to sort by three different aspects
How to use selection sort to sort by three different aspects

Time:11-15

I have a student class that consists of students

  1. last name
  2. first name
  3. birthdate
  4. id
  5. course
  6. phone number

I have to use selection sort to sort the given students' list by these three properties:

  1. birthdates
  2. last names
  3. first names

I don't even know where to start, because all the examples online show how to sort an array of numbers. This is all I have now and it is definitely not correct.

        public void MinMax(StudentsRegister other)
    {
        
        StudentsRegister AllYearStudents = new StudentsRegister();
        for (int i = 0; i < AllYearStudents.StudentsCount() - 1; i  )
        {
            int smallest = i;
            for (int j = i   1; j < AllYearStudents.StudentsCount(); j  )
            {
                if (AllYearStudents.Get(j) < other.Get(smallest))
                {
                    smallest = j;
                }
            }
        }
    }

CodePudding user response:

OK, so this is clearly homework so I'm not going to do it, but I don't mind giving pointers. You'll have to fill in the blanks

Your code as you have it seems like part of a reasonable selectionsort algo, except it doesn't have the swapping code. Here's an algo I borrowed from tutorialspoint.com:

for (int i = 0; i < n - 1; i  ) {
   smallest = i;
   for (int j = i   1; j < n; j  ) {
      if (arr[j] < arr[smallest]) {
         smallest = j;
      }
   }
   temp = arr[smallest];
   arr[smallest] = arr[i];
   arr[i] = temp;
}

It doesnt really matter what the array is of, so long as arr[j] < arr[smallest] works out.. You've only seen it as an array of ints but if you had an array of students all you want to know is "is this student lower than that student, according to ....". Thing is, for this assignment you have to vary what fields are compared during the "student X is less than student Y" option

Instead of doing arr[j] < arr[smallest], what about if we called a method called IsLessThan .. it could take two students and compare their last names:

public bool IsLessThan(Student x, Student y){
  return x.LastName.CompareTo(y.LastName)<0;
}

Remember, all we wanted was a boolean decision "is x less than y" - with ints, that's "2 is less than 3". With last names that's "jones is less than smith".

for (int i = 0; i < n - 1; i  ) {
   smallest = i;
   for (int j = i   1; j < n; j  ) {
      if ( IsLessThan(arr[j], arr[smallest]) ) { //look, I've swapped out one bool test for another bool test
         smallest = j;
      }
   }
   temp = arr[smallest];
   arr[smallest] = arr[i];
   arr[i] = temp;
}

Get this working with just an array of students (not your fancy StudentsRegister class), and just sorting by last name..

Now.. when that works, have a think about "how could we expand it so that it works by firstname, lastname or birthdate?". What about if we had some variable somewhere that IsLessThan could access, and if that variable was one thing, then lastname would be comparted, and if the variable was another thing, then firstname would be compared, and if the variable was a third thing then birthdate would be compared. This means the switching is all built into IsLessThan

Once you get that done, maybe you'll fancy having a crack at a polymorphism approach, creating a StudentComparer that has an IsLessThan method.. and then a StudentBirthdateComparer that extends StudentComparer and overrides the IsLessThan and compares the birthdays, and another StudentLastNameComparer that extends the StudentComparer and overrides IsLessThan with a lastname compare..

But go in stages, otherwise you'll get confused and lost

  • Related