Skip to content

Instantly share code, notes, and snippets.

@Midi12
Last active August 17, 2021 16:56
Show Gist options
  • Save Midi12/31fb81b4e92256f298afd01687cad3e2 to your computer and use it in GitHub Desktop.
Save Midi12/31fb81b4e92256f298afd01687cad3e2 to your computer and use it in GitHub Desktop.
bogosort in dart
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);
}
@WasserEsser
Copy link

WasserEsser commented Aug 17, 2021

Thank you for this, I knew it wouldn't be so hard to implement a good sorting algorithm in Dart.

@Midi12
Copy link
Author

Midi12 commented Aug 17, 2021

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