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