希尔排序实现(ShellSort) Java数据结构与算法
2010-07-18 16:36
633 查看
希尔排序实现(ShellSort) Java数据结构与算法
源代码:
源代码:
/** * * @author sunnyykn */ class ArraySh { private long[] theArray; //ref to array theArray private int nElems; //number of data items public ArraySh(int max) //constructor { theArray = new long[max]; //create the array nElems = 0; //no items yet } public void insert(long value) //put element into array { theArray[nElems] = value; //insert it nElems ++; //increment size } public void display() //displays array contents { System.out.print("A = "); for(int j = 0;j < nElems;j ++) System.out.print(theArray[j] + " "); System.out.println(""); } public void shellSort() { int inner,outer; long temp; int h = 1; while(h <= nElems/3) h = h*3 + 1; //(1,4,13,40,121...) while(h > 0) { 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; } h = (h -1) / 3; } } } class ShellSortApp { public static void main(String[] args) { int maxSize = 100; ArraySh arr; arr = new ArraySh(maxSize); for(int j = 0;j < maxSize ;j ++ ) { long n = (int)(java.lang.Math.random()*99); arr.insert(n); } arr.display(); arr.shellSort(); arr.display(); } }
相关文章推荐
- 希尔排序(shellsort)算法实现
- PHP实现排序算法----希尔排序(Shell Sort)
- 【内部排序】三:希尔排序(Shell Sort)的多种实现(不断优化+源码)
- 用python实现希尔排序(shell_sort)
- 希尔排序(Shell Sort)——插入排序法(Java实现)
- 数据算法之希尔排序(shellSort)的Java实现
- 希尔排序(shellsort)算法实现
- 希尔排序(shellsort)算法实现 推荐
- 排序算法--希尔排序(Shell Sort)_C#程序实现
- 数据算法之希尔排序(shellSort)的Java实现
- 排序--希尔排序的实现(shellsort)
- 希尔排序(shellsort)算法实现
- C++实现谢尔排序(希尔排序)(shell sort)
- 算法----希尔排序(shell sort)
- 希尔排序(ShellSort)
- 希尔排序(shell sort)
- [排序] 希尔排序(Shell Sort)
- 【排序算法 】希尔排序 shell sort(插入类排序)
- 常用排序算法的c++实现(冒泡,选择,插入,堆,shell,快速,归并 )与sort()对比
- 排序 —— 希尔排序(Shell sort)