Last active
June 28, 2020 22:25
-
-
Save viiiprock/6e3a0e3e993a93cf384b98c2c721cc00 to your computer and use it in GitHub Desktop.
Algorithms
This file contains hidden or 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 selectionSort(arr) { | |
| for (let i = 0; i < arr.length; i++) { | |
| // find the smallest index | |
| let minIndex = i; | |
| for (let j = i + 1; j < arr.length; j++) { | |
| if (arr[minIndex] > arr[j]) { | |
| minIndex = j; | |
| } | |
| } | |
| // swap the index | |
| if (minIndex !== i) { | |
| var temp = arr[i]; | |
| arr[i] = arr[minIndex]; | |
| arr[minIndex] = temp; | |
| // use ES6 destruction array to swap | |
| // [arr[i], arr[minIndex]]=[arr[minIndex], arr[i]] | |
| } | |
| } | |
| return arr; | |
| } | |
| const arr = [45, 58, 11, 25, 34, 32, 97, 58]; | |
| selectionSort(arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment