Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222499
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-09-01 08:44:12

  1. /***********************************************************************
  2.   * Compilation: javac Merge.java
  3.   * Execution: java Merge
  4.   * An bare-bone NlogN implementation of mergesort.
  5. **************************************************************************/
  6. public class Merge {
  7.     
  8.     public static void sort(Comparable[] a) {
  9.         sort(a, 0, a.length);
  10.     }
  11.     
  12.     public static void sort(Comparable[] a, int lo, int hi) {
  13.         int N = hi - lo; // size of subarray
  14.         if (N <= 1) return; // 0 or 1 sub-array, so we're done
  15.         
  16.         // recursively sort left and right halves
  17.         int mid = lo + N/2;
  18.         sort (a, lo, mid);
  19.         sort (a, mid, hi);
  20.         
  21.         // merge two sorted subarrays
  22.         int i = lo, j = mid;
  23.         Comparable[] aux = new Comparable[N];
  24.         for (int k = 0; k < N; k++) {
  25.             if (i == mid) aux[k] = a[j++];
  26.             else if (j == hi) aux[k] = a[i++];
  27.             else if (a[j].compareTo(a[i]) < 0) aux[k] = a[j++];
  28.             else aux[k] = a[i++];
  29.         }
  30.         
  31.         // copy back to array a[]
  32.         for (int k = 0; k < N; k++)
  33.             a[lo+k] = aux[k];
  34.         
  35.     }
  36.     
  37.     /**
  38.      * Check if array is sorted
  39.      * */
  40.     public static boolean isSorted(Comparable[] a) {
  41.         for (int i = 1; i < a.length; i++)
  42.             if (a[i].compareTo(a[i-1]) < 0) return false;
  43.         return true;
  44.     }
  45.  
  46.     public static void show(Comparable[] a) {
  47.         for (int i = 0; i < a.length; i++)
  48.             System.out.println(a[i]);
  49.     }
  50.     
  51.     public static void main(String[] args) {
  52.      int N = Integer.parseInt(args[0]);
  53.      
  54.      // generate N random real number between 0 and 1
  55.      long start = System.currentTimeMillis();
  56.      Double[] a = new Double[N];
  57.      for (int i = 0; i < N; i++)
  58.          a[i] = Math.random();
  59.      long stop = System.currentTimeMillis();
  60.      double elapsed = (stop - start) / 1000.0;
  61.      System.out.println("Generating input: " + elapsed + " seconds");
  62.      
  63.      // sort them
  64.      start = System.currentTimeMillis();
  65.      sort(a);
  66.      stop = System.currentTimeMillis();
  67.      elapsed = (stop - start) / 1000.0;
  68.      System.out.println("Mergesort: " + elapsed + " seconds");
  69.      System.out.println("The array sorted is " + isSorted(a));
  70.      
  71.      if (N < 100) show(a);
  72.     }
  73. }
阅读(386) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~