Shell sort can be considered as an improvement of insertion sort based on two reasons: 1) Insertion sort is efficient if the input is "almost sorted". 2) Insertion sort is typically inefficient because it moves values just one position at a time.
class ShellSortAlgorithm { public void ShellSort(int[] array) { int gap = array.Length / 2; for (int i = gap; i > 0; i /= 2) { // insertion sort for (int j = i; j + i <= array.Length; j += i) //start from i { int key = array[j]; int f = j; while (f - i >= 0 && array[f - i] > key) { array[f] = array[f - i]; f -= i; } array[f] = key; } } } }
本文介绍了一种基于插入排序的改进算法——希尔排序,并详细解释了其如何通过增大元素之间的间隔来提高排序效率。希尔排序特别适用于初始数据接近有序的情况,能够显著减少比较和交换的次数。
483

被折叠的 条评论
为什么被折叠?



