Skip to content

Commit f0d38a5

Browse files
cleanup code
1 parent 1bc1273 commit f0d38a5

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class BubbleSortNotifier extends SortingNotifier {
1313
steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.compared)); // external
1414

1515
if (arr[j] > arr[j + 1]) {
16-
steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.swapped)); // external
16+
steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.swiping)); // external
1717
final tmp = arr[j];
1818
arr[j] = arr[j + 1];
1919
arr[j + 1] = tmp;
2020
isSorted = false;
2121
}
2222

23-
steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.unSorted)); // external
23+
steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.swiped)); // external
2424
}
2525

2626
steps.add(SortingStep(
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import 'package:algorithm_visualizer/features/sorting/base/view_model/sorting_notifier.dart';
2+
import 'package:collection/collection.dart';
23

34
class InsertionSortNotifier extends SortingNotifier {
4-
55
@override
66
List<SortingStep> buildSorting(List<int> values) {
7-
// TODO: implement buildSorting
8-
throw UnimplementedError();
7+
final steps = <SortingStep>[];
8+
final arr = List<int>.from(values);
9+
10+
for (int i = 1; i < arr.length; i++) {
11+
int j = i;
12+
13+
while (j > 0 && arr[j] < arr[j - 1]) {
14+
steps.add(SortingStep(index1: j, index2: j - 1, action: SortingStatus.compared));
15+
steps.add(SortingStep(index1: j, index2: j - 1, action: SortingStatus.swiping));
16+
17+
arr.swap(j, j - 1);
18+
steps.add(SortingStep(index1: j, index2: j - 1, action: SortingStatus.swiped));
19+
j--;
20+
}
21+
steps.add(SortingStep(index1: j, index2: j, action: SortingStatus.sorted));
22+
}
23+
24+
steps.add(SortingStep(index1: arr.length - 1, index2: arr.length - 1, action: SortingStatus.sorted));
25+
26+
return steps;
927
}
1028
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SelectionSortNotifier extends SortingNotifier {
1414

1515
if (arr[j] < arr[minIndex]) {
1616
final previousIndex = minIndex;
17+
// to reset action
1718
if (minIndex != i) {
1819
steps.add(SortingStep(index1: previousIndex, index2: previousIndex, action: SortingStatus.none));
1920
}
@@ -24,12 +25,12 @@ class SelectionSortNotifier extends SortingNotifier {
2425
}
2526

2627
if (minIndex != i) {
27-
steps.add(SortingStep(index1: i, index2: minIndex, action: SortingStatus.swapped));
28+
steps.add(SortingStep(index1: i, index2: minIndex, action: SortingStatus.swiping));
2829

2930
final temp = arr[i];
3031
arr[i] = arr[minIndex];
3132
arr[minIndex] = temp;
32-
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.unSorted));
33+
steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.swiped));
3334
}
3435

3536
steps.add(SortingStep(index1: i, index2: i, action: SortingStatus.sorted));

0 commit comments

Comments
 (0)