Shell Sort是就地比较排序算法。 Shell Sort是插入排序的一般化,允许交换相距较远的项目。 该算法通过对彼此远离的元素对进行排序来执行初步工作。 该算法逐渐减少了要比较的元素之间的间隔,因为目标是减少整个数组中元素的移动量。 随着间隔减小到1,该算法变得与插入排序算法相同。
Algorithm Classification
下表包含有关Shell Sort Sort算法分析的信息。 它根据时间复杂度以及空间复杂度定义了最坏,平均和最佳情况。
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.