Skip to content

Instantly share code, notes, and snippets.

@o-henry
Last active February 3, 2020 05:47
Show Gist options
  • Save o-henry/487d8137d4cfc5db756d38cde55eadf6 to your computer and use it in GitHub Desktop.
Save o-henry/487d8137d4cfc5db756d38cde55eadf6 to your computer and use it in GitHub Desktop.
Selection Sort
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