class E9Bonus {
  // the main program
  public static void main(String[] args) {
    // create several persons and store them in an array
     System.out.println("This is E9Bonus Program"); 
     Person[] ppl = new Person[6];
     for (int i =0;i<ppl.length;i++) {
       // creating a new person with random weight
       ppl[i] = new Person("p"+(i+1) , 180-Math.random()*10*i);
     }
     System.out.println("Before Sorting");
     for (int i=0; i<ppl.length;i++) {
       System.out.println(ppl[i]);
     }
     
     Person.selectSort(ppl);
     
     System.out.println("After sorting");
     for (int i=0; i<ppl.length;i++) {
       System.out.println(ppl[i]);
     }
     
  }
}
class Person { 
  // fields
  private String name;
  private double weight;
  
  // constructor
  public Person(String n, double w) { name = n; weight = w; }
  
  // return 1 if the weight is bigger than the person's weight compared to 
  // return -1 if the weight is less than the person's weight compared to
  // return 0 if they are equal
  public int compareTo(Person other) {
    if (this.weight > other.weight) 
      return 1;
    else if (this.weight < other.weight) 
      return -1;
    else 
      return 0;
  }
  
  public String toString() {
    return "Person name: " +name+" weight :"+weight;
  }
  // sort the array in ascending order using select order algorithm
  public static void selectSort(Person[] p) {
    // Loop through the array, finding the next minimum value &
    // swapping it with the first elt. in the unsorted part of the array.
    for (int i = 0; i < p.length - 1; i++ ) {
      int minIndex = i;  // index of min value
      Person min = p[minIndex]; // min value
      // Find the minimum value in the unsorted part of the array.
      for ( int j = i + 1; j < p.length; j++ ) {
        // If this element is less than min it becomes the min.
        if ( p[j].compareTo(min) < 0 ) {
          minIndex = j;
          min = p[minIndex];
        }
      }
      // Swap the first item in the unsorted part of the array
      // with the minimum item (even if the first item is the min).
      Person temp = p[minIndex];
      p[minIndex] = p[i];
      p[i] = temp;
    }
  }

  
}