close







Shell sort Java Code,Print 過程




立即點擊


publicclasstest{privatelong[]data;privateintlen;publictest(intmax){data=newlong[max];len=0;}publicvoidinsert(longvalue){data[len]=value;len++;}publicvoiddisplay(){System.out.print("Data:");for(intj=0;j0)//decreasingh,untilh=1{//h-sortthefilefor(outer=h;outerh-1&&data[inner-h]>=temp){data[inner]=data[inner-h];inner-=h;}data[inner]=temp;}h=(h-1)/3;//decreaseh}}publicstaticvoidmain(String[]args){intmaxSize=10;testarr=newtest(maxSize);for(intj=0;j




請參考我的做法 public class Y0114 { private long[] data; private static int step = 1; private int len; public Y0114(int max) { data = new long[max]; len = 0; } public void insert(long value){ data[len] = value; len++; } public void display() { System.out.print("Data:"); for (int j = 0; j < len; j++) System.out.print(data[j] + " "); System.out.println(""); } public void test() { int inner, outer; long temp; //find initial value of h int h = 1; while (h <= len / 3) h = h * 3 + 1; // (1, 4, 13, 40, 121, ...) while (h > 0) // decreasing h, until h=1 { // h-sort the file for (outer = h; outer < len; outer++) { temp = data[outer]; inner = outer; // one subpass (eg 0, 4, 8) while (inner > h - 1 && data[inner - h] >= temp) { data[inner] = data[inner - h]; inner -= h; } data[inner] = temp; // display each step System.out.print("[Step " + step++ + "] "); System.out.println("h=" + h + "; inner=" + inner + "; outer=" + outer); System.out.println(java.util.Arrays.toString(data)); } h = (h - 1) / 3; // decrease h } } public static void main(String[] args) { int maxSize = 10; Y0114 arr = new Y0114(maxSize); for (int j = 0; j < maxSize; j++) { long n = (int) (java.lang.Math.random() * 99); arr.insert(n); } arr.display(); arr.test(); arr.display(); } }








以上文章來自奇摩知識家,如有侵犯請留言告知


https://tw.answers.yahoo.com/question/index?qid=20111216000010KK00114

C902071D09AF7660
arrow
arrow
    文章標籤
    章來自 上文 奇摩 j++
    全站熱搜

    雞排妹伴遊 發表在 痞客邦 留言(0) 人氣()