add dart examples for chapter 1 and 2 (#209)

* add dart example for chapter 1

* add dart example for chapter 2
This commit is contained in:
N3-M3-S1S
2022-11-19 00:31:23 +03:00
committed by GitHub
parent cd38dc146d
commit b3a143a3ae
2 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
main() {
print(selectionSort([5, 3, 6, 2, 10]));
}
List<int> selectionSort(List<int> arr) {
final List<int> newArr = List.empty(growable: true);
final arrLength = arr.length;
for (int i = 0; i < arrLength; i++) {
final smallest = findSmallest(arr);
newArr.add(arr.removeAt(smallest));
}
return newArr;
}
int findSmallest(List<int> arr) {
int smallest = arr[0];
int smallestIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
smallestIndex = i;
}
}
return smallestIndex;
}