Java中的插入排序

插入排序是一种排序算法,可一次构建一个最终的排序数组(或列表)。 该算法遍历列表并删除当前元素,在列表的排序部分中找到位置,然后将其插入到列表中。 重复此过程,直到对整个列表进行排序。

Algorithm Classification

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

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

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

Insertion Sort In Java

public final class InsertionSort {

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


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

        for (int unsortIndex = 1; unsortIndex < arrayLength;  unsortIndex++) {
            int newElement = collection[unsortIndex];
            int iterator = 0;

            for (iterator = unsortIndex; iterator > 0 && collection[iterator - 1] > newElement; iterator--) {
                collection[iterator] = collection[iterator - 1];
            }
            collection[iterator] = newElement;
        }
    }
}
Sample Code (GitHub)

The details of the InsertionSort class can be viewed here.

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

Conclusion

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

The post Insertion Sort in Java appeared first on Code2Bits.

from: https://dev.to//code2bits/insertion-sort-in-java-50h7

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值