6.shellalgorithm.java

来自「Java各种排序算法代码」· Java 代码 · 共 50 行

JAVA
50
字号
/**
 * A Shellsort demonstration algorithm
 * ShellAlgorithm.java, 19.04.97
 *
 * @author   Lars Marius Garshol
 * @version  1.00 - 19.04.97
 */
/**
 * JAVA爱好者:http://www.javafan.net
 * 免费提供Java教程、电子书籍、代码源码、开发工具等
 * 站长后注
 */
class ShellAlgorithm extends SortAlgorithm {
  void sort(int a[]) throws Exception {
    int h[]={109,41,19,5,1}; //Best Sedgewick sort increment sequence
    int tmp,j;
    int incno; //Increment number

    //Find right start increment
    for (tmp=0; h[tmp]>a.length; tmp++)
      ;

    //Loop through increment sequence 
    for (incno=tmp; incno<=h.length-1; incno++) {

      for (int i=h[incno]; i<=a.length-1; i++) {
        // Invariant: a[start..i-h[incno]] h[incno]-sorted
    
        tmp=a[i];   
        for (j=i-h[incno]; j>=0 && a[j]>tmp; j=j-h[incno]) {
          if (stopRequested) {
            return;
          }

          a[j+h[incno]]=a[j];		        

          pause(j,i);
        }
 
        //Now we've found a[i]'s place
        a[j+h[incno]]=tmp;

      } //for i

    } //for incno

  } //end of sort
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?