Skip to content

Instantly share code, notes, and snippets.

@AlexKenbo
Last active January 15, 2021 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexKenbo/1eeb5a37c32680cf2ad1a820adbc2b82 to your computer and use it in GitHub Desktop.
Save AlexKenbo/1eeb5a37c32680cf2ad1a820adbc2b82 to your computer and use it in GitHub Desktop.
Сортировка выбором O(n х n)
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