Created
July 22, 2021 10:43
-
-
Save dimasandhk/5b8e91d76ddd03f6b4c72a76876b6997 to your computer and use it in GitHub Desktop.
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
extension Sorting on List<int> { | |
List<int> sortAsc() { | |
List<int> list = this; // Selection sort algorithm | |
int length = this.length; | |
for (int i = 0; i < length - 1; i++) { | |
int min = i; | |
for (int j = i + 1; j < length; j++) { | |
if (list[j] < list[min]) { | |
min = j; | |
} | |
} | |
int tmp = list[min]; | |
list[min] = list[i]; | |
list[i] = tmp; | |
} | |
return list; | |
} | |
} | |
void main() { | |
List<int> arr = [3, 2, 1, 4, 5]; | |
print(arr); | |
List<int> sortedArr = arr.sortAsc(); | |
print(sortedArr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment