zoukankan      html  css  js  c++  java
  • 基于插入排序的objectsort的java实现

      1 package com.liu.Sort;
      2 
      3 public class objectSort {
      4     public static void main(String[] args)
      5     {
      6         int maxsize = 100;
      7         ArrayInOb arr;
      8         arr = new ArrayInOb(maxsize);
      9         
     10         arr.insert("evans", "hello", 12);
     11         arr.insert("liu", "yangchao", 43);
     12         arr.insert("wang", "shuo", 4);
     13         arr.insert("allience", "good", 54);
     14         arr.insert("we", "lgd", 87);
     15         arr.insert("ig", "dk", 32);
     16         arr.insert("find", "abondan", 45);
     17         arr.insert("alyin", "deby", 73);
     18         arr.insert("phythe", "c", 19);
     19         arr.insert("javascript", "c#", 23);
     20         
     21         arr.display();
     22         arr.insertSort();
     23         System.out.println();
     24         arr.display();
     25         
     26         arr.insertSortAge();
     27         System.out.println();
     28         arr.display();
     29     }
     30 }
     31 
     32 class ArrayInOb
     33 {
     34     private Person[] a;
     35     private int nElems;
     36     public ArrayInOb(int max)
     37     {
     38         a = new Person[max];
     39         nElems = 0;
     40     }
     41     
     42     public void insert(String last,String first,int age)
     43     {
     44         a[nElems] = new Person(last,first,age);
     45         nElems++;
     46     }
     47     
     48     public void display()
     49     {
     50         for(int j=0;j<nElems;j++)
     51             a[j].display();
     52         
     53     }
     54     
     55     public void insertSort()
     56     {
     57         int in,out;
     58         for(out=1;out<nElems;out++)
     59         {
     60             in=out;
     61             Person temp = a[in];
     62             while(in>0&&a[in-1].getLast().compareTo(temp.getLast())>0)
     63             {
     64                 a[in]=a[in-1];
     65                 --in;
     66             }
     67             a[in] = temp;
     68         }
     69     }
     70     
     71     public void insertSortAge()
     72     {
     73         int in,out;
     74         for(out=1;out<nElems;out++)
     75         {
     76             in=out;
     77             Person temp = a[in];
     78             while(in>0&&a[in-1].getAge()>temp.getAge())
     79             {
     80                 a[in]=a[in-1];
     81                 --in;
     82             }
     83             a[in] = temp;
     84         }
     85     }
     86 }
     87 
     88 class Person
     89 {
     90     private String lastname;
     91     private String firstname;
     92     private int age;
     93     public Person(String last,String first,int a)
     94     {
     95         lastname = last;
     96         firstname = first;
     97         age = a;
     98     }
     99     
    100     public void display()
    101     {
    102         System.out.print("last name:"+lastname);
    103         System.out.print(",first name:"+firstname);
    104         System.out.println(",age:"+age);
    105     }
    106     
    107     public String getLast()
    108     {
    109         return lastname;
    110     }
    111 
    112     public String getLastname() {
    113         return lastname;
    114     }
    115 
    116     public void setLastname(String lastname) {
    117         this.lastname = lastname;
    118     }
    119 
    120     public String getFirstname() {
    121         return firstname;
    122     }
    123 
    124     public void setFirstname(String firstname) {
    125         this.firstname = firstname;
    126     }
    127 
    128     public int getAge() {
    129         return age;
    130     }
    131 
    132     public void setAge(int age) {
    133         this.age = age;
    134     }
    135     
    136     
    137 }
  • 相关阅读:
    5-29
    5-28
    5-27
    -5-26
    5-25
    5-24
    5-21
    RabbitMQ消息中间件极速入门与实战
    细说java多线程之内存可见性
    全面解析java注解
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3656834.html
Copyright © 2011-2022 走看看