Last active
August 17, 2021 16:56
-
-
Save Midi12/31fb81b4e92256f298afd01687cad3e2 to your computer and use it in GitHub Desktop.
bogosort in dart
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
extension IsSorted<T extends Comparable> on List<T> { | |
bool get isSorted { | |
if (this.length <= 1) return true; | |
for (var i = 1; i < this.length; i++) { | |
if (this[i].compareTo(this[i - 1]) < 0) return false; | |
} | |
return true; | |
} | |
} | |
void bogosort<T extends Comparable>(List<T> list) { | |
while (!list.isSorted) { | |
list.shuffle(); | |
} | |
} | |
void main() { | |
var test = <int>[0, 1, 6, -5, 10, 3.958.toInt()]; | |
bogosort(test); | |
print(test); | |
} |
Thank you for this, I knew it wouldn't be so hard to implement a good sorting algorithm in Dart.
You’re welcome :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this, I knew it wouldn't be so hard to implement a good sorting algorithm in Dart.