public Object clone() { SeqList o = null; try { o = (SeqList) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } // r == o.r 此函数没有新生成数组 // System.arraycopy(r, 0, o.r, 0, len); // System.out.println("\n==============="+(r==o.r)); // 生成新的数组 o.r = Arrays.copyOf(r, len + 1); // System.out.println(o.r.length+".....");//测试clone复制了多长的数组 // System.out.println("\n==============="+(r==o.r)); return o; }
public static void main(String[] args) { // 检测内存消耗 Runtime rt_jmm = Runtime.getRuntime(); long total_jmm = rt_jmm.totalMemory(); long max_jmm = rt_jmm.maxMemory(); int MB_JMM = 1024 * 1024; System.out.println("total : " + total_jmm / MB_JMM + " mb"); System.out.println("max : " + max_jmm / MB_JMM + " mb"); // 初始空闲内存记录 long free1_jmm = rt_jmm.freeMemory(); /*小数据*/ int[] d = {50, 10, 90, 30, 70, 40, 80, 60, 20, 33}; int N = d.length; SeqList q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10; q0 = new SeqList(d, N); q1 = (SeqList) q0.clone(); // clone的时候,只clone了len长度的数组 q2 = (SeqList) q0.clone(); q3 = (SeqList) q0.clone(); q4 = (SeqList) q0.clone(); q5 = (SeqList) q0.clone(); q6 = (SeqList) q0.clone(); q7 = (SeqList) q0.clone(); q8 = (SeqList) q0.clone(); q9 = (SeqList) q0.clone(); q10 = (SeqList) q0.clone(); q0.toString("排序之前", false); BubbleSort0(q0); q0.toString("交换排序", true); BubbleSort1(q1); q1.toString("一般冒泡", true); BubbleSort2(q2); q2.toString("改进冒泡", true); SelectSort(q3); q3.toString("选择排序", true); InsertSort(q4); q4.toString("插入排序", true); ShellSort(q5); q5.toString("希尔排序", true); HeapSort(q6); q6.toString("哦堆排序", true); MergeSort1(q7, q7, 1, N); // System.gc(); q7.toString("迭代归并", true); MergeSort2(q8); q8.toString("非迭归并", true); QuickSort1(q9, 1, N); q9.toString("快速排序", true); QuickSort2(q10, 1, N); q10.toString("改进快速", true); /*大数据*/ int[] d1 = new int[KB]; // 4KB for (int i = 0; i < KB; ++i) { d1[i] = (int) (100 * Math.random()); } SeqList l1 = new SeqList(d1, KB); SeqList l2 = (SeqList) l1.clone(); long free2_jmm = rt_jmm.freeMemory(); // 在不用MergeSort的情况下,此处如果没有用q0,则内存被回收,输出7,加上下面的语句,则输出11 // System.out.println(q0.r.length+",,,,"); SeqList l3 = (SeqList) l1.clone(); SeqList l4 = (SeqList) l1.clone(); SeqList l5 = (SeqList) l1.clone(); SeqList l6 = (SeqList) l1.clone(); SeqList l7 = (SeqList) l1.clone(); SeqList l8 = (SeqList) l1.clone(); SeqList l9 = (SeqList) l1.clone(); SeqList l10 = (SeqList) l1.clone(); SeqList l11 = (SeqList) l1.clone(); System.out.println("used : " + (free1_jmm - free2_jmm) / MB_JMM + " mb"); long start1 = System.currentTimeMillis(); BubbleSort0(l1); long end1 = System.currentTimeMillis(); long time1 = end1 - start1; long start2 = System.currentTimeMillis(); BubbleSort1(l2); long end2 = System.currentTimeMillis(); long time2 = end2 - start2; long start3 = System.currentTimeMillis(); BubbleSort2(l3); long end3 = System.currentTimeMillis(); long time3 = end3 - start3; long start4 = System.currentTimeMillis(); SelectSort(l4); long end4 = System.currentTimeMillis(); long time4 = end4 - start4; long start5 = System.currentTimeMillis(); InsertSort(l5); long end5 = System.currentTimeMillis(); long time5 = end5 - start5; long start6 = System.currentTimeMillis(); ShellSort(l6); long end6 = System.currentTimeMillis(); long time6 = end6 - start6; long start7 = System.currentTimeMillis(); HeapSort(l7); long end7 = System.currentTimeMillis(); long time7 = end7 - start7; free1_jmm = rt_jmm.freeMemory(); long start8 = System.currentTimeMillis(); MergeSort1(l8, l8, 1, KB); long end8 = System.currentTimeMillis(); long time8 = end8 - start8; free2_jmm = rt_jmm.freeMemory(); System.out.println("迭代归并used : " + (free1_jmm - free2_jmm) / MB_JMM + " mb"); free1_jmm = rt_jmm.freeMemory(); long start9 = System.currentTimeMillis(); MergeSort2(l9); long end9 = System.currentTimeMillis(); long time9 = end9 - start9; free2_jmm = rt_jmm.freeMemory(); System.out.println("非迭代归并used : " + (free1_jmm - free2_jmm) / MB_JMM + " mb"); long start10 = System.currentTimeMillis(); QuickSort1(l10, 1, KB); long end10 = System.currentTimeMillis(); long time10 = end10 - start10; long start11 = System.currentTimeMillis(); QuickSort2(l11, 1, KB); long end11 = System.currentTimeMillis(); long time11 = end11 - start11; System.out.println("冒泡1\t|冒泡2\t|冒泡3\t|选择\t|插入\t" + "|希尔\t|堆\t|迭归\t|非归\t|快排\t|改快"); System.out.println( time1 + "\t|" + time2 + "\t|" + time3 + "\t|" + time4 + "\t|" + time5 + "\t|" + time6 + "\t|" + time7 + "\t|" + time8 + "\t|" + time9 + "\t|" + time10 + "\t|" + time11); }