Last active
February 3, 2020 05:47
-
-
Save o-henry/487d8137d4cfc5db756d38cde55eadf6 to your computer and use it in GitHub Desktop.
Selection Sort
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
const selectionSort = arr => { | |
let minIndex; | |
let temp; | |
// 데이터가 담긴 배열 전체를 돕니다. | |
for (let i = 0; i < arr.length - 1; i++) { | |
minIndex = i; // 맨 처음부터 확인합니다. | |
for (let j = i + 1; j < arr.length; j++) { | |
// 최소값 위치를 찾습니다. | |
if (arr[j] < arr[minIndex]) { | |
minIndex = j; | |
} | |
} | |
temp = arr[minIndex]; // 최소값을 저장합니다. | |
arr[minIndex] = arr[i]; | |
arr[i] = temp; | |
} | |
return arr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment