Skip to content

Commit d163c4c

Browse files
cleanup sorting logic
1 parent bf5be93 commit d163c4c

File tree

1 file changed

+17
-68
lines changed

1 file changed

+17
-68
lines changed
Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,42 @@
11
import 'package:algorithm_visualizer/features/sorting/base/view_model/sorting_notifier.dart';
2-
import 'package:collection/collection.dart';
3-
import 'package:flutter/material.dart';
42

53
class SelectionSortNotifier extends SortingNotifier {
64
@override
7-
Future<void> buildSort() async {
8-
final list = List<SortableItem>.from(state.list);
9-
final values = list.map((e) => e.value).toList();
10-
11-
final steps = selectionSortSteps(values);
12-
13-
for (final step in steps) {
14-
if (operation != SortingEnum.played) return;
15-
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);
21-
22-
await Future.delayed(speedDuration);
23-
24-
break;
25-
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);
30-
31-
await Future.delayed(speedDuration);
32-
33-
list.swap(step.index1, step.index2);
34-
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;
39-
40-
state = state.copyWith(list: list, positions: positions);
41-
break;
42-
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;
48-
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;
55-
}
56-
57-
await Future.delayed(speedDuration);
58-
}
59-
60-
await greenSortedItemsAsDone();
61-
}
62-
63-
List<SortingStep> selectionSortSteps(List<int> values) {
5+
List<SortingStep> buildSorting(List<int> values) {
646
final steps = <SortingStep>[];
657
final arr = List<int>.from(values);
668

679
for (int i = 0; i < arr.length - 1; i++) {
6810
int minIndex = i;
6911

7012
for (int j = i + 1; j < arr.length; j++) {
71-
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.compared)); // external
13+
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.compared));
7214

7315
if (arr[j] < arr[minIndex]) {
74-
// steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.swapped)); // external
16+
final previousIndex = minIndex;
17+
if (minIndex != i) {
18+
steps.add(SortingStep(index1: previousIndex, index2: previousIndex, action: SortingStatus.none));
19+
}
7520
minIndex = j;
7621
}
7722

78-
// steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.unSorted)); // external
23+
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.unSorted));
7924
}
8025

81-
steps.add(SortingStep(index1: i, index2: minIndex, action: SortingStatus.swapped)); // external
26+
if (minIndex != i) {
27+
steps.add(SortingStep(index1: i, index2: minIndex, action: SortingStatus.swapped));
8228

83-
final temp = arr[i];
84-
arr[i] = arr[minIndex];
85-
arr[minIndex] = temp;
29+
final temp = arr[i];
30+
arr[i] = arr[minIndex];
31+
arr[minIndex] = temp;
32+
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.unSorted));
33+
}
8634

87-
// steps.add(SortingStep(
88-
// index1: arr.length - i - 1, index2: arr.length - i - 1, action: SortingStatus.sorted)); // external
35+
steps.add(SortingStep(index1: i, index2: i, action: SortingStatus.sorted));
8936
}
9037

38+
steps.add(SortingStep(index1: arr.length - 1, index2: arr.length - 1, action: SortingStatus.sorted));
39+
9140
return steps;
9241
}
9342
}

0 commit comments

Comments
 (0)