Shell排序Java示例

Nig*_*ker 5 java sorting algorithm shellsort

谁能给我一个shell排序的例子?我是这里的新人,他必须学习shell排序,但首先我必须找到一个Java shell排序示例.我在谷歌找到了一个例子,但这太难了.

Ade*_*lin 15

在这里,这段代码非常简单:

/**
   * Shellsort, using Shell’s (poor) increments.
   * @param a an array of Comparable items.
   */
  public static <T extends Comparable<? super T>>
  void shellsort( T [ ] a )
  {
    int j;
    for( int gap = a.length / 2; gap > 0; gap /= 2 )
    {
      for( int i = gap; i < a.length; i++ )
      {
         T tmp = a[ i ];
         for( j = i; j >= gap && tmp.compareTo( a[ j - gap ] ) < 0; j -= gap )
         {
           a[ j ] = a[ j - gap ];
         }
         a[ j ] = tmp;
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

我从一本名为Java中的数据结构和算法分析的书中偷了它.这本书很容易理解.我建议你阅读它.

  • 感谢您发布此内容!这是所有答案中唯一的REAL shell排序答案.其余的是Knuth的Shell Sort变体,我确信差距/ 2.2答案只是浪费时间. (3认同)

sgo*_*les 9

可能是,这个java代码会对你有所帮助.

 public class ShellSort {
       private long[] data;

      private int len;

      public ShellSort(int max) {
        data = new long[max];
        len = 0;
      }

      public void insert(long value){
        data[len] = value; 
        len++;
      }

      public void display() {
        System.out.print("Data:");
        for (int j = 0; j < len; j++)
          System.out.print(data[j] + " ");
        System.out.println("");
      }

      public void shellSort() {
        int inner, outer;
        long temp;
        //find initial value of h
        int h = 1;
        while (h <= len / 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 < len; outer++) {
            temp = data[outer];
            inner = outer;
            // one subpass (eg 0, 4, 8)
            while (inner > h - 1 && data[inner - h] >= temp) {
              data[inner] = data[inner - h];
              inner -= h;
            }
            data[inner] = temp;
          }
          h = (h - 1) / 3; // decrease h
        }
      }

      public static void main(String[] args) {
        int maxSize = 10;
        ShellSort arr = new ShellSort(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();
      }
    }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢先生。但是我还是新手,我不明白要学习这个java (2认同)

yko*_*tor 5

Shell排序通过比较由多个位置的间隙分隔的元素改进插入排序.

这使得元素可以朝着预期的位置迈出"更大的步伐".使用越来越小的间隙尺寸对数据进行多次传递.Shell排序的最后一步是普通插入排序,但到那时,数据数组几乎保证排序.

此代码可以帮助您更好地理解逻辑.

package Sorts;
public class ShellSort extends Sorter{

@Override
public <T extends Comparable<? super T>> void sort(T[] a) {
    int h = 1;
    while((h*3+1) < a.length)
        h = 3*h+1;
    while(h > 0){
        for(int i = h-1; i < a.length; i++){
            T s = a[i];
            int j = i;
            for(j = i; (j>=h) && (a[j-h].compareTo(s) > 0); j-=h)
                a[j] = a[j-h];
            a[j] = s;
        }
        h /= 3;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是Knuth排序。除此之外,我看到的唯一问题是算法中未遵循h应小于等于a.length / 3的规则。实际上应该是h &lt;= Math.ceil(a.length / 3),但似乎没有人算。 (2认同)

Tek*_*lia 2

这是一个例子:

public static void shellsort( Comparable [ ] a )
    {
        for( int gap = a.length / 2; gap > 0;
                     gap = gap == 2 ? 1 : (int) ( gap / 2.2 ) )
            for( int i = gap; i < a.length; i++ )
            {
                Comparable tmp = a[ i ];
                int j = i;

                for( ; j >= gap && tmp.compareTo( a[ j - gap ] ) < 0; j -= gap )
                    a[ j ] = a[ j - gap ];
                a[ j ] = tmp;
            }
    }
Run Code Online (Sandbox Code Playgroud)