Skip to content

Commit bd000c9

Browse files
handle selection sort logic
1 parent 59cc7bc commit bd000c9

File tree

1 file changed

+66
-28
lines changed

1 file changed

+66
-28
lines changed

lib/features/sorting/selection/view_model/selection_sort_notifier.dart

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,88 @@ class SelectionSortNotifier 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 (int i = 0; i < list.length - 1; i++) {
11+
final steps = selectionSortSteps(values);
12+
13+
for (final step in steps) {
1114
if (operation != SortingEnum.played) return;
1215

13-
int minIndex = i;
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);
1421

15-
for (int j = i + 1; j < list.length; j++) {
16-
if (operation != SortingEnum.played) return;
22+
await Future.delayed(speedDuration);
1723

18-
list[minIndex] = list[minIndex].copyWith(sortedStatus: SortingStatus.compared);
19-
list[j] = list[j].copyWith(sortedStatus: SortingStatus.compared);
20-
state = state.copyWith(list: list);
21-
await Future.delayed(speedDuration);
24+
break;
2225

23-
if (list[j].value < list[minIndex].value) minIndex = j;
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);
29+
state = state.copyWith(list: list);
2430

25-
list[j] = list[j].copyWith(sortedStatus: SortingStatus.unSorted);
26-
list[minIndex] = list[minIndex].copyWith(sortedStatus: SortingStatus.unSorted);
27-
state = state.copyWith(list: list);
28-
}
31+
await Future.delayed(speedDuration);
2932

30-
if (minIndex != i) {
31-
list[minIndex] = list[minIndex].copyWith(sortedStatus: SortingStatus.swapped);
32-
list[i] = list[i].copyWith(sortedStatus: SortingStatus.swapped);
33-
state = state.copyWith(list: list);
34-
await Future.delayed(speedDuration);
33+
list.swap(step.index1, step.index2);
3534

36-
list.swap(i, minIndex);
35+
final positions = Map<int, Offset>.from(state.positions);
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;
3739

38-
final positions = Map<int, Offset>.from(state.positions);
39-
final temp = positions[list[i].id]!;
40-
positions[list[i].id] = positions[list[minIndex].id]!;
41-
positions[list[minIndex].id] = temp;
40+
state = state.copyWith(list: list, positions: positions);
41+
break;
4242

43-
state = state.copyWith(list: list, positions: positions);
44-
await Future.delayed(speedDuration);
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);
46+
state = state.copyWith(list: list);
47+
break;
4548

46-
list[i] = list[i].copyWith(sortedStatus: SortingStatus.unSorted);
47-
list[minIndex] = list[minIndex].copyWith(sortedStatus: SortingStatus.unSorted);
48-
state = state.copyWith(list: list);
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);
53+
state = state.copyWith(list: list);
54+
break;
4955
}
56+
57+
await Future.delayed(speedDuration);
5058
}
5159

5260
await greenSortedItemsAsDone();
5361
}
5462

63+
List<SortingStep> selectionSortSteps(List<int> values) {
64+
final steps = <SortingStep>[];
65+
final arr = List<int>.from(values);
66+
67+
for (int i = 0; i < arr.length - 1; i++) {
68+
int minIndex = i;
69+
70+
for (int j = i + 1; j < arr.length; j++) {
71+
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.compared)); // external
72+
73+
if (arr[j] < arr[minIndex]) {
74+
// steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.swapped)); // external
75+
minIndex = j;
76+
}
77+
78+
// steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.unSorted)); // external
79+
}
80+
81+
steps.add(SortingStep(index1: i, index2: minIndex, action: SortingStatus.swapped)); // external
82+
83+
final temp = arr[i];
84+
arr[i] = arr[minIndex];
85+
arr[minIndex] = temp;
86+
87+
// steps.add(SortingStep(
88+
// index1: arr.length - i - 1, index2: arr.length - i - 1, action: SortingStatus.sorted)); // external
89+
}
90+
91+
return steps;
92+
}
5593
}

0 commit comments

Comments
 (0)