Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Last active March 4, 2018 23:25
Show Gist options
  • Save Spuffynism/a30b89732993b29f4c55e0fec1ec7a48 to your computer and use it in GitHub Desktop.
Save Spuffynism/a30b89732993b29f4c55e0fec1ec7a48 to your computer and use it in GitHub Desktop.
js list sorts
let selectionSort = (t) => {
for (let i = 0; i < t.length; i++) {
let min = i;
for (let j = i + 1; j < t.length; j++) {
if(t[j] < t[min])
min = j;
}
[t[min], t[i]] = [t[i], t[min]];
}
return t;
};
let insertionSort = (a) => {
for (let i = 0; i < a.length; i++)
for (let j = i; j > 0 && a[j] < a[j - 1]; j--)
[a[j - 1], a[j]] = [a[j], a[j - 1]];
return a;
};
let fasterInsertionSort = (a) => {
for(let i = 1, j; i < a.length; i++) {
let j;
let tmp = a[i];
for (j = i; j > 0 && tmp < a[j - 1]; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
return a;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment