package com.util.sort; public class ShellSort { private long[]theArray; private int nElem; public ShellSort(int max){ theArray = new long[max]; nElem = 0; } //插入元素 public void insert(long value){ theArray[nElem] = value; nElem++; } //打印元素 public void display(){ for (int i = 0;i<theArray.length;i++) { System.out.print(theArray[i]+" "); } System.out.println(); } public void sort(){ System.out.println("MMMMMMMMMMM"); int inner = 0; int outer = 0; long temp = 0; int h = 1; while (h <= nElem/3) h = h*3+1; while(h > 0){ for (outer = h;outer<nElem;outer++) { temp = theArray[outer]; inner = outer; while (inner > h-1 && theArray[inner-h] >= temp) { theArray[inner] = theArray[inner-h]; inner -= h; } theArray[inner] = temp; } h = (h-1)/3; } } }