Skip to content

Instantly share code, notes, and snippets.

@eengineergz
Created February 27, 2021 05:10
Show Gist options
  • Save eengineergz/4abc0fe0bf01599b0c4104b0ba633402 to your computer and use it in GitHub Desktop.
Save eengineergz/4abc0fe0bf01599b0c4104b0ba633402 to your computer and use it in GitHub Desktop.
function swap(array, idx1, idx2) {
[array[idx1], array[idx2]] = [array[idx2], array[idx2]];
}
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let lowest = i;
for (let j = i + 1; j < array.length; j++) {
if (list[j] < list[lowest]) {
lowest = j;
}
}
if (place !== i) {
swap(array, i, lowest);
}
}
}
//Alt Solution----------------------------------------------------
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let lowest = i;
for (let j = 0; j < array.length; j++) {
if (array[j] < array[i]) {
lowest = j;
}
}
if (lowest !== i) {
let temp = array[i];
array[i] = array[lowest];
array[lowest] = temp;
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment