Java中的选择排序算法

选择排序是一种排序算法,特别是就地比较排序,用于对整数数组进行排序。 该算法将输入列表分为两个部分:已排序和未排序。 该算法在未排序部分中查找最大的元素,并将其与未排序分区中的最后一个位置交换。

Algorithm Classification

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

分类值类排序算法数据结构数组时间复杂度:最佳Ω(n2)时间复杂度:平均值Θ(n2)时间复杂度:最差上2)空间复杂性:最差O(1)

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

Selection Sort In Java

public final class SelectionSort {

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

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

        for (int unsortIndex = arrayLength - 1; unsortIndex > 0; unsortIndex--) {
            int largest = 0;
            for (int i = 1; i <= unsortIndex; i++) {
                if (collection[i] > collection[largest]) {
                    largest = i;
                }
            }
            swap(collection, largest, unsortIndex);
        }
    } 

    private void swap(int[] collection, int x, int y) {
        int temp = collection[x];
        collection[x] = collection[y];
        collection[y] = temp;
    }   
}
Sample Code (GitHub)

The details of the SelectionSort class can be viewed here.

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

Conclusion

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

The post Selection Sort Algorithm In Java appeared first on Code2Bits.

from: https://dev.to//code2bits/selection-sort-algorithm-in-java-331a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值