Created with <3 with dartpad.dev.
Last active
October 21, 2023 00:41
-
-
Save HBFLEX/67007542027638221b15e97b1f20b82a to your computer and use it in GitHub Desktop.
keen-spray-1020
This file contains hidden or 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(){ | |
List<int> numbers = [10, 4, 2, 6, 3, 30]; | |
final stopWatch = Stopwatch(); | |
stopWatch.start(); | |
insertionSort(numbers); | |
stopWatch.stop(); | |
for(int n in numbers) {print(n);} | |
print('It took ${stopWatch.elapsedMilliseconds} ms time to sort'); | |
} | |
void insertionSort(List<int> arr){ | |
final int length = arr.length; | |
int current; | |
int j; | |
for(int i = 1; i < length; i++){ | |
current = arr[i]; | |
j = i - 1; | |
while(j >=0 && arr[j] > current){ | |
arr[j + 1] = arr[j]; | |
j--; | |
} | |
arr[j + 1] = current; | |
} | |
} | |
void selectionSort(List<int> arr){ | |
int length = arr.length; | |
int lowest = arr[0]; | |
int temp; | |
for(int i = 0; i < length; i++){ | |
for(int j = i + 1; j < length; j++){ | |
if(arr[i] > arr[j]){ | |
lowest = arr[j]; | |
temp = arr[i]; | |
arr[i] = lowest; | |
arr[j] = temp; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment