希尔排序
来源:互联网 发布:手机淘宝自定义模块 编辑:程序博客网 时间:2024/05/28 18:44
希尔排序是基于插入排序的,插入排序缺点:复制的次数太多,希尔排序通过加大插入排序中元素之间的间隔,并在这些有间隔的元素中进行插入排序,从而是数据项能大跨度地移动。当这些数据项排过一趟序后,希尔排序算法减小数据项的间隔再进行排序,依此进行下去。进行这些排序时数据项之间的间隔被称为增量。
package sun.simpleSort;// shellSort.java// demonstrates shell sort// to run this program: C>java ShellSortApp//--------------------------------------------------------------class ArraySh1 { private long[] theArray; // ref to array theArray private int nElems; // number of data items//-------------------------------------------------------------- public ArraySh1(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++) // for each element, System.out.print(theArray[j] + " "); // display it System.out.println(""); }//-------------------------------------------------------------- public void shellSort() { int inner, outer; long temp; int h = 1; // find initial value of h while(h <= nElems/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<nElems; outer++) { temp = theArray[outer]; inner = outer; // one subpass (eg 0, 4, 8) while(inner > h-1 && theArray[inner-h] >= temp) { theArray[inner] = theArray[inner-h]; inner -= h; } theArray[inner] = temp; } // end for h = (h-1) / 3; // decrease h } // end while(h>0) } // end shellSort()//-------------------------------------------------------------- } // end class ArraySh////////////////////////////////////////////////////////////////public class ShellSortApp1 { public static void main(String[] args) { int maxSize = 20; // array size ArraySh1 arr; arr = new ArraySh1(maxSize); // create the array for(int j=0; j<maxSize; j++) // fill array with { // random numbers long n = (int)(java.lang.Math.random()*99); arr.insert(n); } arr.display(); // display unsorted array arr.shellSort(); // shell sort the array arr.display(); // display sorted array } // end main() } // end class ShellSortApp////////////////////////////////////////////////////////////////
0 0
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 希尔排序
- 六:取六个字的行 20140910 ①文本处理
- 七:取七个字的行 20140911 ①文本处理
- 经验及技巧:windows phone 8.1 获得经纬度
- HDU 2087 kmp
- 一张图解决一道笔试题 --- 理解fork函数
- 希尔排序
- JAVA Web简单的用户名密码验证
- BZOJ 2662 BeiJing wc2012 冻结
- ajax 初识 w3cschool
- magento “include(Mage/Customemail/Helper/Data.php) cant open the file”
- 脏读、不可重复读、幻读
- 寻找一个字符串中最长的重复子串
- gbd调试 错误 illegal instruction 4
- 图册 -大图显示原理