Skip to content

Commit 064379f

Browse files
cleanup BubbleSortNotifier
1 parent be00981 commit 064379f

File tree

1 file changed

+72
-36
lines changed

1 file changed

+72
-36
lines changed

lib/features/sorting/bubble/view_model/bubble_sort_notifier.dart

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,99 @@ class BubbleSortNotifier extends SortingNotifier {
66
@override
77
Future<void> buildSort() async {
88
final list = List<SortableItem>.from(state.list);
9+
final values = list.map((e) => e.value).toList();
910

10-
for (i = 0; i < list.length - 1; i++) {
11-
if (operation != SortingEnum.played) return;
11+
final steps = bubbleSortSteps(values);
1212

13-
bool isSorted = true;
13+
for (final step in steps) {
14+
if (operation != SortingEnum.played) return;
1415

15-
for (j = 0; j < list.length - i - 1; j++) {
16-
if (operation != SortingEnum.played) return;
16+
switch (step.action) {
17+
case SortingStatus.compared:
18+
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.compared);
19+
list[step.index2] = list[step.index2].copyWith(sortedStatus: SortingStatus.compared);
20+
state = state.copyWith(list: list);
1721

18-
list[j] = list[j].copyWith(sortedStatus: SortingStatus.compared);
19-
list[j + 1] = list[j + 1].copyWith(sortedStatus: SortingStatus.compared);
20-
state = state.copyWith(list: list);
22+
await Future.delayed(speedDuration);
2123

22-
if (operation != SortingEnum.played) return;
23-
await Future.delayed(speedDuration);
24-
if (operation != SortingEnum.played) return;
25-
await Future.delayed(speedDuration);
24+
break;
2625

27-
if (list[j].value > list[j + 1].value) {
28-
list[j] = list[j].copyWith(sortedStatus: SortingStatus.swapped);
29-
list[j + 1] = list[j + 1].copyWith(sortedStatus: SortingStatus.swapped);
26+
case SortingStatus.swapped:
27+
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.swapped);
28+
list[step.index2] = list[step.index2].copyWith(sortedStatus: SortingStatus.swapped);
3029
state = state.copyWith(list: list);
31-
if (operation != SortingEnum.played) return;
3230

3331
await Future.delayed(speedDuration);
3432

35-
isSorted = false;
36-
list.swap(j, j + 1);
33+
list.swap(step.index1, step.index2);
3734

3835
final positions = Map<int, Offset>.from(state.positions);
39-
final tempPosition = positions[list[j].id]!;
40-
positions[list[j].id] = positions[list[j + 1].id]!;
41-
positions[list[j + 1].id] = tempPosition;
36+
final tempPosition = positions[list[step.index1].id]!;
37+
positions[list[step.index1].id] = positions[list[step.index2].id]!;
38+
positions[list[step.index2].id] = tempPosition;
4239

4340
state = state.copyWith(list: list, positions: positions);
44-
await Future.delayed(speedDuration);
45-
if (operation != SortingEnum.played) return;
41+
break;
4642

47-
list[j] = list[j].copyWith(sortedStatus: SortingStatus.unSorted);
48-
list[j + 1] = list[j + 1].copyWith(sortedStatus: SortingStatus.unSorted);
43+
case SortingStatus.unSorted:
44+
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.unSorted);
45+
list[step.index2] = list[step.index2].copyWith(sortedStatus: SortingStatus.unSorted);
4946
state = state.copyWith(list: list);
47+
break;
5048

51-
await Future.delayed(speedDuration);
52-
53-
if (operation != SortingEnum.played) return;
54-
} else {
55-
list[j] = list[j].copyWith(sortedStatus: SortingStatus.unSorted);
56-
list[j + 1] = list[j + 1].copyWith(sortedStatus: SortingStatus.unSorted);
49+
// i don't want to make it green while sorting and mark all of them at once as green at the end
50+
case SortingStatus.sorted:
51+
case SortingStatus.none:
52+
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.none);
5753
state = state.copyWith(list: list);
58-
}
54+
break;
5955
}
6056

61-
if (isSorted) {
62-
await greenSortedItemsAsDone();
63-
return;
64-
}
57+
await Future.delayed(speedDuration);
6558
}
59+
6660
await greenSortedItemsAsDone();
6761
}
62+
63+
List<BubbleSortStep> bubbleSortSteps(List<int> values) {
64+
final steps = <BubbleSortStep>[];
65+
final arr = List<int>.from(values);
66+
67+
for (int i = 0; i < arr.length - 1; i++) {
68+
bool isSorted = true;
69+
70+
for (int j = 0; j < arr.length - i - 1; j++) {
71+
steps.add(BubbleSortStep(index1: j, index2: j + 1, action: SortingStatus.compared)); // external
72+
73+
if (arr[j] > arr[j + 1]) {
74+
steps.add(BubbleSortStep(index1: j, index2: j + 1, action: SortingStatus.swapped)); // external
75+
final tmp = arr[j];
76+
arr[j] = arr[j + 1];
77+
arr[j + 1] = tmp;
78+
isSorted = false;
79+
}
80+
81+
steps.add(BubbleSortStep(index1: j, index2: j + 1, action: SortingStatus.unSorted)); // external
82+
}
83+
84+
steps.add(BubbleSortStep(
85+
index1: arr.length - i - 1, index2: arr.length - i - 1, action: SortingStatus.sorted)); // external
86+
87+
if (isSorted) break;
88+
}
89+
90+
return steps;
91+
}
92+
}
93+
94+
class BubbleSortStep {
95+
final int index1;
96+
final int index2;
97+
final SortingStatus action;
98+
99+
BubbleSortStep({
100+
required this.index1,
101+
required this.index2,
102+
required this.action,
103+
});
68104
}

0 commit comments

Comments
 (0)