对多个日期排序,然后用二分查找法查找相应的日期(SXT)

 对多个日期排序,然后用二分查找法查找相应的日期(SXT)

 

  1. /*
  2.  * 对多个日期排序,然后用二分查找法查找相应的日期(SXT)
  3.  * 
  4.  */
  5. public class TestDateSort
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         Date[] days = new Date[5];
  10.         days[0] = new Date(200654);
  11.         days[1] = new Date(200674);
  12.         days[2] = new Date(200854);
  13.         days[3] = new Date(200459);
  14.         days[4] = new Date(200454);
  15.         bubbleSort(days); //对日期排序
  16.         //打印排好序的结果
  17.         for (int i = 0; i < days.length; i++)
  18.         {
  19.             System.out.println(days[i]);
  20.         }
  21.        
  22.         //查找2006, 7, 4
  23.         Date d = new Date(200674);
  24.         String str = String.valueOf(d);
  25.         
  26.         System.out.println(binarySearch(days, d));
  27.         
  28.     }
  29.     //对日期排序
  30.     public static Date[] bubbleSort(Date[] a)
  31.     {
  32.         int len = a.length;
  33.         for (int i = len - 1; i >= 1; i--)
  34.         {
  35.             for (int j = 0; j <= i - 1; j++)
  36.             {
  37.                 if (a[j].compare(a[j + 1]) > 0)
  38.                 {
  39.                     Date temp = a[j];
  40.                     a[j] = a[j + 1];
  41.                     a[j + 1] = temp;
  42.                 }
  43.             }
  44.         }
  45.         return a;
  46.     }
  47.     
  48.     //二分查找法
  49.     public static int binarySearch(Date[] days, Date d)
  50.     {
  51.         if (days.length == 0)
  52.             return -1;
  53.         int startPos = 0;                 //起始位置
  54.         int endPos = days.length - 1;     //结尾位置
  55.         int m = (startPos + endPos) / 2;  //二分点
  56.         
  57.         while (startPos <= endPos)
  58.         {
  59.             if (d.compare(days[m]) == 0)  //比较日期,等于表示找到
  60.                 return m;
  61.             if (d.compare(days[m]) > 0)   //大于0,向后半部分继续二分
  62.             {
  63.                 startPos = m + 1
  64.             }
  65.             if (d.compare(days[m]) < 0)   //小于0,向前半部分继续二分
  66.             {
  67.                 endPos = m - 1;
  68.             }
  69.             m = (startPos + endPos) / 2;
  70.         }
  71.         return -1;
  72.     }
  73. }
  74. //日期比较
  75. class Date
  76. {
  77.     int year, month, day;
  78.     Date(int y, int m, int d)
  79.     {
  80.         year = y;
  81.         month = m;
  82.         day = d;
  83.     }
  84.     //比较日期
  85.     /*
  86.      * 1表示大于,0表示等于,-1表示小于 如果传入的年,大于当前的年,返回1,否则返回-1;
  87.      * 如果传入的月,大于当前的月,返回1,否则返回-1; 如果传入的日,大于当前的日,返回1,否则返回-1;
  88.      */
  89.     public int compare(Date date)
  90.     {
  91.         return year > date.year ? 1 : year < date.year ? -1
  92.                 : month > date.month ? 1 : month < date.month ? -1
  93.                         : day > date.day ? 1 : day < date.day ? -1 : 0;
  94.     }
  95.     //重写toString
  96.     public String toString()
  97.     {
  98.         return "Year:Month:Day -- " + year + "-" + month + "-" + day;
  99.     }
  100. }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值