인프런 워밍업 클럽 스터디 2기 CS (특별미션)

인프런 워밍업 클럽 스터디 2기 CS (특별미션)

function quickSort(arr, left, right) {
  if (left <= right) {
    let pivotIndex = divide(arr, left, right);
    quickSort(arr, left, pivotIndex - 1);
    quickSort(arr, pivotIndex + 1, right);
  }
}

function divide(arr, left, right) {
  let pivot = arr[left].count;
  let leftStart = left + 1;
  let rightStart = right;

  while (leftStart <= rightStart) {
    while (leftStart <= right && pivot >= arr[leftStart].count) {
      leftStart += 1;
    }

    while (rightStart >= left + 1 && pivot <= arr[rightStart].count) {
      rightStart -= 1;
    }

    if (leftStart <= rightStart) {
      swap(arr, leftStart, rightStart);
    }
  }

  swap(arr, left, rightStart);

  return rightStart;
}

function swap(arr, a, b) {
  let temp = arr[a];
  arr[a] = arr[b];
  arr[b] = temp;
}

let user1 = {
  name: '홍길동',
  count: 5,
};

let user2 = {
  name: '임꺽정',
  count: 4,
};

let user3 = {
  name: '이순신',
  count: 3,
};

let user4 = {
  name: '나',
  count: 1,
};

let user5 = {
  name: '짱구',
  count: 5,
};

let arr = [user1, user2, user3, user4, user5];

quickSort(arr, 0, arr.length - 1);

console.log(arr);

댓글을 작성해보세요.

채널톡 아이콘