버블정렬

WooBuntu·2021년 3월 8일
0

JS 90제

목록 보기
27/33
function solution(unsorted) {
  for (let i = 0; i < unsorted.length - 1; i++) {
    for (let j = 0; j < unsorted.length - i - 1; j++)
      if (unsorted[j] > unsorted[j + 1])
        [unsorted[j], unsorted[j + 1]] = [unsorted[j + 1], unsorted[j]];
  }
  return unsorted;
}

const result = solution([13, 5, 11, 7, 23, 15]);
console.log(result);
  • 인접한 원소 간에 비교를 하다 보니 아무래도 선택 정렬보다는 swap을 많이 할 수밖에 없다.

0개의 댓글