`
jiava9900
  • 浏览: 85078 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Shell Sort in Java

阅读更多

    <span style="background-color: #ffffff;"><span style="font-family: 'Times New Roman'; line-height: 18px;">基本思想:</span><br style=""><span style=""> ??? 先取一个小于n的整数d</span><sub style="">1</sub><span style="">作为第一个增量,把文件的全部记录分成d</span><sub style="">1</sub><span style="">个组。所有距离为d</span><sub style="">l</sub><span style="">的倍数的记录放在同一个组中。先在各组内进行直接插人排序;然后,取第二个增量d</span><sub style="">2</sub><span style=""><d</span><sub style="">1</sub><span style="">重复上述的分组和排序,直至所取的增量d</span><sub style="">t</sub><span style="">=1(d</span><sub style="">t</sub><span style=""><d</span><sub style="">t-l</sub><span style=""><…<d</span><sub style="">2</sub><span style=""><d</span><sub style="">1</sub><span style="">),即所有记录放在同一组中进行直接插入排序为止。?</span><span style="">该方法实质上是一种分组插入方法。</span></span>




import java.util.Random;

/**
 * Shell sort in Java.
 * @author Sun Kui
 */
public class ShellSort {
    private long[] theArr;
    private int nElems;

    public ShellSort(int max) {
        theArr = new long[max];
        nElems = 0;
    }

    public void insert(long value) {
        theArr[nElems++] = value;
    }

    public void display() {
        System.out.print("A=");
        for (int j = 0; j < nElems; j++) {
            System.out.print(theArr[j] + " ");
            if (j % 10 == 0) {
                System.out.println();
            }
        }
        System.out.println();
    }

    public void shellSort() {
        int inner, outer;
        long temp;

        // select increment in, start from 1
        int h = 1;
        while(h <= nElems / 3) {
            h = h * 3 + 1;
        }

        while (h > 0) {
            for (outer = h; outer < nElems; outer++) {
                temp = theArr[outer];
                inner = outer;

                while (inner > h - 1 &amp;&amp; theArr[inner - h] >= temp) {
                    theArr[inner] = theArr[inner - h];
                    inner -= h;
                }
                theArr[inner] = temp;
            }
            // decrease increment in each step
            h = (h - 1) / 3;
        }
    }

    public static void main(final String... args) {
        Random rand = new Random();
        ShellSort ss = new ShellSort(100);
        for (int i = 0; i < 100; i++) {
            ss.insert(rand.nextInt(10000));
        }
        ss.shellSort();
        ss.display();
    }
}


?

end

 
0
0
分享到:
评论

相关推荐

    Advanced Topics in Java

    The emphasis is not on teaching advanced concepts of the Java language, per se, ...trees, advanced sorting methods (heapsort, quicksort, mergesort, Shell sort), and hashing (a very fast way to search).

    java 各种算法集合

    在这段代码中,`ShellSort`类实现了`SortUtil.Sort`接口,并重写了其中的`sort`方法。该方法通过递减的增量序列进行分组排序,每组内部使用插入排序。这种方法能够有效地减少数据的移动次数,提高排序效率。 以上...

    Java经典面试题-算法.docx

    public class ShellSort implements SortUtil.Sort{ public void sort(int[] data) { for(int i=data.length/2;i&gt;2;i/=2){ for(int j=0;j;j++){ insertSort(data,j,i); } } insertSort(data,0,1); } ...

    java 排序 面试题

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i &gt; 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    各种排序算法java实现

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i &gt; 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    用Java实现几种常见的排序算法

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i &gt; 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    Java常见排序算法.pdf

    希尔排序类(ShellSort)实现SortUtil接口。它的sort方法使用了增量序列来分组排序,并在最后调用insertSort方法进行最终的插入排序以达到完全排序的效果。 这些排序算法在Java中的实现为学习和理解各种排序算法的...

    java数组排序

    public class ShellSort { public static void sort(int[] array) { int gap = array.length / 2; while (gap &gt; 0) { for (int i = gap; i ; i++) { int temp = array[i]; int j; for (j = i; j &gt;= gap && ...

    各类排序算法java的实现

    public class ShellSort implements SortUtil.Sort{ public void sort(int[] data) { for(int i = data.length / 2; i &gt; 2; i /= 2) { for(int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    java常用算法 代码

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i &gt; 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    用Java实现几种常见的排序算法.txt

    根据提供的文件信息,我们可以总结出该文档主要涉及了五种基于Java实现的排序算法:插入排序(Insert Sort)、冒泡排序(Bubble Sort)、选择排序(Selection Sort)、希尔排序(Shell Sort)以及快速排序(Quick ...

    JAVA四种基本排序.txt

    public static void shellSort(int[] data) { int n = data.length; int gap = n / 2; while (gap &gt; 0) { for (int i = gap; i ; i++) { int temp = data[i]; int j = i; while (j &gt;= gap && data[j - gap] &gt;...

    各种排序算法 JAVA代码实现

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i &gt; 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    各种排序算法比较(java实现)

    3. **希尔排序(Shell Sort)**: 希尔排序是对插入排序的改进,通过设定一个间隔序列,将待排序的元素分组,对每组使用插入排序,逐步减小间隔直到为1,最终完成排序。希尔排序的时间复杂度在最坏情况下为O(n^1.5),...

    java 常用排序算法

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for(int i=data.length/2; i&gt;2; i/=2){ for(int j=0; j; j++){ insertSort(data, j, i); } } insertSort(data, 0, 1); ...

    各种排序Java的实现

    public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i &gt; 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...

    java排序

    public void shellSort(int[] data) { int inner, outter, temp; int h = 1; while (h ) { h = h * 3 + 1; } while (h &gt; 0) { for (outter = h; outter ; outter++) { temp = data[outter]; inner = outter...

Global site tag (gtag.js) - Google Analytics