Java中的Shell Sort算法

Shell Sort是就地比较排序算法。 Shell Sort是插入排序的一般化,允许交换相距较远的项目。 该算法通过对彼此远离的元素对进行排序来执行初步工作。 该算法逐渐减少了要比较的元素之间的间隔,因为目标是减少整个数组中元素的移动量。 随着间隔减小到1,该算法变得与插入排序算法相同。

Algorithm Classification

下表包含有关Shell Sort Sort算法分析的信息。 它根据时间复杂度以及空间复杂度定义了最坏,平均和最佳情况。

属性值类排序算法类ification内部,原位,不稳定算法数据结构数组时间复杂度:最佳Ω(n log(n))时间复杂度:平均值Θ(n log2n)时间复杂度:最差上2)空间复杂性:最差O(1)

Please use the following link for an explanation on Big-O notation and what is good, fair and bad.

Shell Sort In Java

public final class ShellSort {

    public void sort(int[] collection) {
        if (collection != null) {
            shellSort(collection);
        } else {
            throw new IllegalArgumentException("Input paramenter for array to sort is null.");
        }
    }

    private void shellSort(int[] collection) {
        int arrayLength = collection.length;

        for (int gap = arrayLength / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arrayLength; i++) {
                int newElement = collection[i];

                int j = i;
                while (j >= gap && collection[j - gap] > newElement) {
                    collection[j] = collection[j - gap];
                    j -= gap;
                }
                collection[j] = newElement;
            }
        }
    } 
}
Sample Code (GitHub)

The details of the Shell Sort class can be viewed here.

The details of the Shell Sort JUnit Test class can be viewed here.

Conclusion

Shell排序算法构成了较大的一组排序算法的一部分。 通过经验学习是我创建这篇关于Java中Shell Sort算法实现的文章的原因。 我了解了很多其他人如何解决其他语言(包括Java中的不同实现)的Shell Sort算法的知识。

The post Shell Sort Algorithm in Java appeared first on Code2Bits.

from: https://dev.to//code2bits/shell-sort-algorithm-in-java-4hek

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值