Last active
January 15, 2021 10:51
-
-
Save AlexKenbo/1eeb5a37c32680cf2ad1a820adbc2b82 to your computer and use it in GitHub Desktop.
Сортировка выбором O(n х n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
var rng = new Random(); | |
var testList = List<int>.generate(10, (_) => rng.nextInt(30) - rng.nextInt(10)); | |
print(testList); | |
print(selectionSort(testList)); | |
} | |
int findSmallest(List arr) { | |
var smallest = arr[0]; | |
var smallestIndex = 0; | |
for(var e in arr) { | |
if(e < smallest) { | |
smallest = e; | |
smallestIndex = arr.indexOf(e); | |
} | |
} | |
return smallestIndex; | |
} | |
List selectionSort(List arr) { | |
var newArr = []; | |
var copyArr = List<int>.from(arr); | |
for(var _ in copyArr) { | |
var smallestIndex = findSmallest(arr); | |
//print('$e $smallestIndex'); | |
newArr.add(arr[smallestIndex]); | |
arr.removeAt(smallestIndex); | |
} | |
return newArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment