-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_example.dart
62 lines (54 loc) · 1.48 KB
/
sorting_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
abstract interface class SortStrategy {
List<int> sort(List<int> list);
}
class BubbleSortStrategy implements SortStrategy {
@override
List<int> sort(List<int> list) {
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list.length - i - 1; j++) {
if (list[j] > list[j + 1]) {
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
return list;
}
}
class SelectionSortStrategy implements SortStrategy {
@override
List<int> sort(List<int> list) {
for (int i = 0; i < list.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (list[j] < list[minIndex]) {
minIndex = j;
}
}
int temp = list[minIndex];
list[minIndex] = list[i];
list[i] = temp;
}
return list;
}
}
class SortContext {
SortStrategy _sortStrategy;
SortContext(SortStrategy sortStrategy) : _sortStrategy = sortStrategy;
void changeStrategy(SortStrategy sortStrategy) =>
_sortStrategy = sortStrategy;
List<int> sort(List<int> list) {
return _sortStrategy.sort(list);
}
}
void main() {
// Create SortContext with BubbleSortStrategy
SortContext sortContext = SortContext(BubbleSortStrategy());
List<int> list1 = [5, 2, 9, 1, 5];
sortContext.sort(list1);
// Change strategy to MergeSortStrategy
sortContext.changeStrategy(SelectionSortStrategy());
List<int> list2 = [8, 3, 7, 4, 2];
sortContext.sort(list2);
}