The objective of my program is to sort a file of students in order from a-z using arrays. I'm using selection sort method but for some reason my code is matching what i want. for example:
Enter name of a file of Students: student10.txt

In forward order, students are:
Sally (100)
Rico (82)
Sue (95)
Bill (43)
Rob (67)
Meng (94)
Pam (82)
Harvey (72)
Rich (45)
Debra (88)

Now sorting ...

In forward order, students are:
Debra (88)
Rich (45)
Harvey (72)
Pam (82)
Meng (94)
Rico (82)
Rob (67)
Bill (43)
Sally (100)
Sue (95)
//please help...

import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw21
{
//-----------------------------------------------------------------------
   public static void main (String [] args) throws Exception
   {
      Scanner kb = new Scanner(System.in);

      System.out.print("\nEnter name of a file of Students: ");
      String filename = kb.nextLine();

      Section section = new Section(filename);

      System.out.println("\nIn forward order, students are:");
      section.print(System.out);

      System.out.println("\nNow sorting ...");
      section.sort();

      System.out.println("\nIn forward order, students are:");
      section.print(System.out);
   } 
//-----------------------------------------------------------------------
} // end class Hw21
/////////////////////////////////////////////////////////////////////////
class Section
{
   private Student [] a;
   private int used;
   private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
   public Section ( String filename ) throws Exception
   {
      Scanner sc = new Scanner(new File(filename));

      a = new Student[INIT_SIZE];
      used = 0;

      while ( sc.hasNext() )
      {
         Student s = Student.read(null,sc);

         if ( used == a.length )
         {
            Student[] newA = new Student[2*a.length+1];
            for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
            newA[used] = s;
            used++;
            a = newA;
         }
         else
         {
            a[used] = s;
            used++;
         }
      }
   }
//-----------------------------------------------------------------------
   public void print ( PrintStream ps )
   {
      for ( int i = 0 ; i < used ; i++ )
      {
         ps.println(a[i]);
      }
   }
//-----------------------------------------------------------------------
   public int howMany() { return used; }
//-----------------------------------------------------------------------
   public void sort ()
   {
      for ( int i = 0 ; i < used-1 ; i++ )
      {
			int minIndex = i;
         String last = a[i].getName();
			for(int j = i+1; j<used; j++)
			{
				String next = a[j].getName();
				if (last.compareTo(next)> 0)
				{
					minIndex =j;
  				}
				else
				{
				
				}
				Student temp = a[minIndex];
				a[minIndex]= a[i];
				a[i] = temp;

			}
			
			
		}

   }
//-----------------------------------------------------------------------
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
   private String name;
   private int grade;
//-----------------------------------------------------------------------
   public Student ( String name, int grade )
   {
      this.name = name;
      this.grade = grade;
   }
//-----------------------------------------------------------------------
   public String toString ()
   {
      return name + " (" + grade + ")";
   }
//-----------------------------------------------------------------------
   public String getName() { return name; }
//-----------------------------------------------------------------------
   public int getGrade() { return grade; }
//-----------------------------------------------------------------------
   public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
   public static Student read ( PrintStream ps, Scanner sc ) 
   {
      if ( ps != null ) ps.println("Reading a Student record ...");
      if ( ps != null ) ps.print("Enter the name: ");
      String name = sc.nextLine();
      if ( ps != null ) ps.print("Enter the grade: ");
      int grade = sc.nextInt(); sc.nextLine();
      return new Student(name,grade);
   }
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////

Edited 5 Years Ago by Gsterminator: n/a

Do you have to implement your own sort() method? If you do, then you need to pick a sort algorithm (i.e. bubble sort, gnome sort, etc.) before you can do the sort. If you do not, use those Collection or Array methods as mKorbel and stultuske provided.

OK, comparing your selection sort algorithm with Wikipedia, something doesn't look right...

// Wikipedia
/* a[0] to a[n-1] is the array to sort */
int iPos;
int iMin;  // <-- *** declare outside the loop, but yours is inside the loop
 
/* advance the position through the entire array */
/*   (could do iPos < n-1 because single element is also min element) */
for (iPos = 0; iPos < n; iPos++) {
  /* find the min element in the unsorted a[iPos .. n-1] */
 
  /* assume the min is the first element */
  iMin = iPos;
  /* test against all other elements */
  for (i = iPos+1; i < n; i++)
    {
      /* if this element is less, then it is the new minimum */  
      if (a[i] < a[iMin])
        {
          /* found new minimum; remember its index */
          iMin = i;
        }
    }
 
  /* iMin is the index of the minimum element. Swap it with the current position */
  if ( iMin != iPos )  // <-- *** you do not have this condition check,
  {                    //     *** but you swap elements right away (line 88-90 in your code)
    swap(a, iPos, iMin);
  }
}

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.