Java Algorithms Sort Insertion Sort on custom objects

Description

Java Algorithms Sort Insertion Sort on custom objects


class Person {/*from w  w  w  .j a v  a2 s  .  co m*/
   private String lastName;
   private int age;

   public Person(String last, int a) { // constructor
      lastName = last;
      age = a;
   }

   public void displayPerson() {
      System.out.print("   Last name: " + lastName);
      System.out.println(", Age: " + age);
   }

   public String getLast() // get last name
   {
      return lastName;
   }
}

class MyArray {
   private Person[] a;
   private int nElems;

   public MyArray(int max) {
      a = new Person[max];
      nElems = 0; // no items yet
   }

   public void insert(String last, int age) {
      a[nElems] = new Person(last, age);
      nElems++; // increment size
   }

   public void display() {
      for (int j = 0; j < nElems; j++) // for each element,
         a[j].displayPerson(); // display it
   }

   public void insertionSort() {
      int in, out;

      for (out = 1; out < nElems; out++) {
         Person temp = a[out]; // out is dividing line
         in = out; // start shifting at out

         while (in > 0 && // until smaller one found,
               a[in - 1].getLast().compareTo(temp.getLast()) > 0) {
            a[in] = a[in - 1]; // shift item to the right
            --in; // go left one position
         }
         a[in] = temp; // insert marked item
      }
   }
}

public class Main {
   public static void main(String[] args) {
      int maxSize = 100; // array size
      MyArray arr; // reference to array
      arr = new MyArray(maxSize); // create the array

      arr.insert("E", 24);
      arr.insert("G", 39);
      arr.insert("D", 37);
      arr.insert("S", 37);
      arr.insert("Y", 43);
      arr.insert("H", 21);
      arr.insert("S", 29);
      arr.insert("V", 42);
      arr.insert("V", 22);
      arr.insert("C", 18);

      System.out.println("Before sorting:");
      arr.display();

      arr.insertionSort(); // insertion-sort them

      System.out.println("After sorting:");
      arr.display();
   }
}



PreviousNext

Related