Files
iroute/lib/v9/pages/sort/functions/comb.dart
toly 8fb4bf57d6 v8
2023-11-08 09:35:29 +08:00

34 lines
912 B
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import '../functions.dart';
///梳排序Comb Sort
Future<void> combSort(List<int> src, SortCallback callback) async{
int gap = src.length;
bool swapped = true;
// 当间隔不为1或存在交换时执行循环
while (gap != 1 || swapped == true) {
// 通过缩小间隔来逐步将元素归位
gap = getNextGap(gap);
swapped = false;
for (int i = 0; i < src.length - gap; i++) {
// 如果当前元素大于间隔位置上的元素,则交换它们的位置
if (src[i] > src[i + gap]) {
int temp = src[i];
src[i] = src[i + gap];
src[i + gap] = temp;
swapped = true;
}
// 实现一个延迟,以便在 UI 上展示排序的动画效果。
await callback(src);
}
}
}
int getNextGap(int gap) {
// 根据当前间隔值计算下一个间隔值
gap = (gap * 10) ~/ 13;
if (gap < 1) return 1;
return gap;
}