package datestructure;

public class PersonInsertsort
{
 public static void main(String[] args)
 {
  int maxsize=100;
  PersonArr perarr;
  perarr=new PersonArr(maxsize);
  
  perarr.insert("d", "r", 13);
  perarr.insert("f", "r", 11);
  perarr.insert("k", "r", 12);
  perarr.insert("b", "r", 15);
  perarr.insert("a", "r", 14);
  
  System.out.println("before insertsort:");
  perarr.display();
  
  perarr.insertsort();
  
  System.out.println("after insertsort:");
  perarr.display();
  
 }

}

//////////////////////////////////

package datestructure;

public class PersonArr
{
 private Person[] a;
 private int nElems;
 
 public PersonArr(int max)
 {
  a=new Person[max];
  nElems=0;
 }
 
 public void insert(String last,String first,int age)
 {
  a[nElems]=new Person(last,first,age);
  nElems++;
 }
 
 public void insertsort()
 {
  for(int j=1;j<nElems;j++)
  {
   Person key=a[j];
   int i=j;
   
   while(i>0&&a[i-1].getlastname().compareTo(key.getlastname())>0)
   {
    a[i]=a[i-1];
    i--;
   }
   a[i]=key;
   
  }
 }
 
 public void display()
 {
  for(int j=0;j<nElems;j++)
   a[j].displayPerson();
  System.out.println("");
 }
 
}

/////////////////////////////

package datestructure;

public class Person
{
 private String firstname;
 private String lastname;
 private long age;
 
 public  Person(String last,String first,long a)
 {
  firstname=first;
  lastname=last;
  age=a;
 }
 
 public void displayPerson()
 {
  System.out.print("lastname:"+lastname);
  System.out.print(",firstname:"+firstname);
  System.out.println(",age"+age);
 }
 
 public String getlastname()
 {
  return lastname;
 }
 
}

posted on 2014-03-14 15:46  growAriel  阅读(310)  评论(0编辑  收藏  举报