日期:2014-05-16 浏览次数:20463 次
public class NormalSort { private static int array[] = new int[10000000000]; private static int base[] = new int[10]; public static void main(String[] args) { init(); long t = System.currentTimeMillis(); sort(); sysout(t); } private static void sysout(long t) { for(int i =0; i < base.length; i ++){ System.out.println(base[i]); } System.out.println("cost time =" + (System.currentTimeMillis()-t)); } private static void init() { for(int i =0; i < array.length; i++){ array[i] = (int)Math.round(Math.random() * array.length); } for(int i =0; i< base.length;i++){ base[i] = array[i]; } } private static void sort() { for(int i =base.length - 1; i < array.length; i++){ long min = base[0]; int index = 0; for(int j=0; j< base.length; j++){ if(base[j] < min){ index = j; min = base[j]; } } if(array[i] > min){ int tmp = array[i]; array[i] = base[index]; base[index] = tmp; } } } }
public class HeapSort { private static int array[] = new int[10000000000]; /** * @param args */ public static void main(String[] args) { init(); long t = System.currentTimeMillis(); sort(); System.out.println("cost time " + (System.currentTimeMillis() - t)); } private static void init() { for (int i = 0; i < array.length; i++) { array[i] = (int)Math.round(Math.random() * array.length); } } private static void adjust(int array[], int i, int length) { int child; int temp; for (temp = array[i]; 2 * i + 1 < length; i = child) { child = 2 * i + 1; if (child < length - 1 && array[child + 1] > array[child]) ++child; if (temp < array[child]) { array[i] = array[child]; array[child] = temp; } else break; } } private static void sort() { for (int i = array.length / 2; i >= 0; --i) adjust(array, i, array.length); int tmp; for (int i = array.length - 1; i >= array.length - 11; --i) { tmp = array[i]; array[i] = array[0]; array[0] = tmp; adjust(array, 0, i); } for (int i = array.length - 11; i < array.length; i++) { System.out.println(array[i]); } } }