combSort<E> function
void
combSort<E>(
- List<
E> list, { - int? begin,
- int? end,
- double shrinkFactor = 2.2,
- Comparator<
E> ? compare,
Sorts the list of numbers using the
Comb sort algorithm.
Parameters
listis any list of items to be sorted.- To perform partial sorting, you can specify the
beginorend. beginis the start index of the range to be sorted.- If
beginis negative, range starts at the 0 - If
beginis not below the length of thelist, range will be empty. endis the final index if the range to be sorted. It is exclusive.- If
endis above the length of thelist, it will be ignored. - If
endis negative, the absolute value of it will be subtracted from the length of thelistto determine where the range ends. - If
endis not greater than thebegin, the range will be empty. shrinkFactoris the gap shrink factor for comb sort. For small lists, higher shrink factor works well.compareis a custom compare to order the list elements. If it is null andlistitems are not Comparable, TypeError is thrown.
Details
Comb sort improves bubble sort by eliminating small values near the end of the list, since they slows down bubble sort.
This algorithm considers the index distance between two items (called gap).
Initially the gap is the length of the range to be sorted, and in each
iteration the gap is reduced by a shrinkFactor untill the gap reaches 1.
Since the implementation requires floating point operations, it performs poorly compared to other variations of bubble sort (e.g. cocktail shaker sort).
Complexity: Time O(n^2) | Space O(1)
Implementation
void combSort<E>(
List<E> list, {
int? begin,
int? end,
double shrinkFactor = 2.2,
Comparator<E>? compare,
}) {
int b, e;
int n = list.length;
// Find the range given the parameters.
b = 0;
e = n;
if (begin != null && b < begin) {
b = begin;
}
if (end != null && end < e) {
e = end;
if (e < 0) e += n;
}
if (compare == null) {
combSortDefault(list, b, e, shrinkFactor);
} else {
combSortCustom(list, b, e, shrinkFactor, compare);
}
}