集册 Java实例教程 选择排序对象

选择排序对象

欢马劈雪     最近更新时间:2020-01-02 10:19:05

460
选择排序对象

class Date {

  int year, month, day;
  /**
  n  o  w  j  a  v  a . c o m
  **/


  Date(int y, int m, int d) {

    year = y;

    month = m;

    day = d;

  }


  static void Display(Date obj) {

    System.out.println(obj.year + "/" + obj.month + "/" + obj.day);

  }

}
/** 时 代 J a v a 提 供 **/

public class Main {


  static boolean GreaterThan(Date a, Date b) {

    return ((a.year > b.year) || ((a.year == b.year) && (a.month > b.month))

        || ((a.year == b.year) && (a.month == b.month) && (a.day > b.day)));

  }


  static void swap(Date[] array, int i, int j) {

    Date tmp = array[i];

    array[i] = array[j];

    array[j] = tmp;

  }


  static void SelectionSort(Date[] array) {

    int n = array.length;


    for (int i = 0; i < n - 1; i++)

      for (int j = i + 1; j < n; j++)

        if (GreaterThan(array[i], array[j]))

          swap(array, i, j);

  }


  public static void main(String[] args) {

    Date[] array = new Date[5];


    array[0] = new Date(2008, 06, 01);

    array[1] = new Date(2005, 04, 03);

    array[2] = new Date(2005, 05, 27);

    ar
展开阅读全文