希尔排序

来源:互联网 发布:mac重启后系统被还原 编辑:程序博客网 时间:2024/04/30 15:26
class ArraySh {private long[] theArray;private int nElems;public ArraySh(int max) {theArray = new long[100];for (int i = 100; i >= 1; i--) {theArray[100 - i] = i;}nElems = 100;}public void insert(long value) {theArray[nElems] = value;nElems++;}public void display() {System.out.println("A*");for (int i = 0; i < nElems; i++) {System.out.print(theArray[i] + " ");System.out.println("");}}         /**         * 类似插入排序,只是多了一个h(隔量)         * h(1,4,13,39)         */      public void shellSort() {int inner, outer;long temp;int h = 1;while (h <= nElems / 3)h = h * 3 + 1;while (h > 0) {System.out.println(h);for (outer = h; outer < nElems; outer++) {temp = theArray[outer];inner = outer;while (inner > h - 1 && theArray[inner - h] >= temp) {theArray[inner] = theArray[inner - h];inner -= h;}theArray[inner] = temp;}// end forh = (h - 1) / 3;}// end while(h>0)}}public class ShellSort {public static void main(String[] args) {ArraySh as = new ArraySh(100);as.shellSort();as.display();}}

原创粉丝点击