selection sort

김_리트리버·2021년 5월 15일
0

[알고리즘]

목록 보기
45/47

맨 앞으로 넣을 값을 범위에서 계속해서 선택 해서 채워나가는 정렬


const array = [1, 2, 3, 4, 5];

function selectionSort(array) {
  for (let i = 0; i < array.length - 1; ++i) {
    let maxValueIndex = i;

    for (let j = i + 1; j < array.length; ++j) {
      if (array[maxValueIndex] < array[j]) {
        maxValueIndex = j;
      }
    }

    const temp = array[i];
    array[i] = array[maxValueIndex];
    array[maxValueIndex] = temp;
  }
}

selectionSort(array);

console.log(array);
profile
web-developer

0개의 댓글