Special Sort

WooBuntu·2021년 3월 8일
0

JS 90제

목록 보기
28/33

강의 반영 전 내 풀이

function solution(unsorted) {
  const positive = [];
  const negative = [];
  for (const num of unsorted) num > 0 ? positive.push(num) : negative.push(num);
  return [...negative, ...positive];
}

const result = solution([1, 2, 3, -3, -2, 5, 6, -6]);
console.log(result);

버블 소트를 활용한 풀이

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] > 0 && unsorted[j + 1] < 0)
        [unsorted[j], unsorted[j + 1]] = [unsorted[j + 1], unsorted[j]];
  }
  return unsorted;
}

const result = solution([1, 2, 3, -3, -2, 5, 6, -6]);
console.log(result);

0개의 댓글