|
1 | 1 | import 'package:algorithm_visualizer/features/sorting/base/view_model/sorting_notifier.dart'; |
2 | | -import 'package:collection/collection.dart'; |
3 | | -import 'package:flutter/material.dart'; |
4 | 2 |
|
5 | 3 | class SelectionSortNotifier extends SortingNotifier { |
6 | 4 | @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) { |
64 | 6 | final steps = <SortingStep>[]; |
65 | 7 | final arr = List<int>.from(values); |
66 | 8 |
|
67 | 9 | for (int i = 0; i < arr.length - 1; i++) { |
68 | 10 | int minIndex = i; |
69 | 11 |
|
70 | 12 | 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)); |
72 | 14 |
|
73 | 15 | 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 | + } |
75 | 20 | minIndex = j; |
76 | 21 | } |
77 | 22 |
|
78 | | - // steps.add(SortingStep(index1: j, index2: j + 1, action: SortingStatus.unSorted)); // external |
| 23 | + steps.add(SortingStep(index1: minIndex, index2: j, action: SortingStatus.unSorted)); |
79 | 24 | } |
80 | 25 |
|
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)); |
82 | 28 |
|
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 | + } |
86 | 34 |
|
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)); |
89 | 36 | } |
90 | 37 |
|
| 38 | + steps.add(SortingStep(index1: arr.length - 1, index2: arr.length - 1, action: SortingStatus.sorted)); |
| 39 | + |
91 | 40 | return steps; |
92 | 41 | } |
93 | 42 | } |
0 commit comments