Skip to content

Instantly share code, notes, and snippets.

@olumidayy
Last active May 15, 2020 07:45
Show Gist options
  • Save olumidayy/b87d33563cbedf378ee13a5a8d4f7bf0 to your computer and use it in GitHub Desktop.
Save olumidayy/b87d33563cbedf378ee13a5a8d4f7bf0 to your computer and use it in GitHub Desktop.
void main() {
print(sorted([2.2, 3, 42, 3444, 1, 67, 2, 7, 234, 564, 6, 8]));
}
List<num> sorted(List<num> arr) {
/* This function takes in a List of numbers
* and returns the sorted list in ascending order
*
* NB: Based on the Comb Sort algorithm
* https://en.wikipedia.org/wiki/Comb_sort*/
var l = arr.length, gap = l, shrink = 1.3;
bool sorted = false;
while (!sorted) {
gap ~/= shrink;
if (gap <= 1) {
sorted = true;
gap = 1;
}
for (var index = 0; index < l - gap; index++) {
var sm = gap + index;
// if a particular number is preceded by a number
// less than it
if (arr[index] > arr[sm]) {
// swap the numbers
var swap = arr[index];
arr[index] = arr[sm];
arr[sm] = swap;
sorted = false;
// means not all the items have been sorted yet
}
}
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment