-
-
Save eengineergz/4abc0fe0bf01599b0c4104b0ba633402 to your computer and use it in GitHub Desktop.
This file contains 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
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