Last active
October 10, 2022 12:44
-
-
Save OnlyTarg/2aa922bf1e25992c675d2e51daaa828c 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
//сортировка вставками | |
void main() { | |
final array = <int>[5, 2, 6, 14, 87, 11, 9, 3, 76]; | |
print(array); | |
sort(array); | |
print(array); | |
} | |
List<int> sort(List<int> array) { | |
//main for loop | |
for (int mainIndex = 0; mainIndex < array.length; mainIndex++) { | |
int value = array[mainIndex]; | |
//secondary for loop for sorting values | |
int i = mainIndex - 1; | |
for (; i >= 0; i--) { | |
if (value > array[i]) break; | |
array[i + 1] = array[i]; | |
} | |
array[i + 1] = value; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment