Created
July 12, 2020 07:32
-
-
Save KR1470R/dfd083d8810b74e7217b2e42bf1bf8f9 to your computer and use it in GitHub Desktop.
Selection sort
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
let arr1 = [8,5,9,1,6,0,2] | |
const selectionSort = arr => { | |
for (let i=0;i<arr.length;i++){ | |
let min = i ///// choose first number | |
for (let j=i+1;j<arr.length;j++){ ///// iterate all array | |
if (arr[min] > arr[j]){ | |
min = j ///////////////////// switching indexs if current iteration less current min number | |
} | |
} | |
if (min !== i){ | |
[arr[i],arr[min]] = [arr[min],arr[i]] ///// swaping elements if min is changed | |
} | |
} | |
} | |
selectionSort(arr1) | |
console.log("arr1 finally", arr1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment