반응형
function qsort(arr, start, end) {
  if (start == end) {
    return;
  }
  let left = start;
  let right = end;

  let pivot = arr[left];

  while (left < right) {
    while (arr[right] >= pivot && right > left) {
      right--;
    }

    arr[left] = arr[right];

    while (arr[left] <= pivot && left < right) {
      left++;
    }

    arr[right] = arr[left];

    if (left == right) {
      arr[left] = pivot; // left == right -> arr[right] = pivot;
      qsort(arr, start, right - 1);
      qsort(arr, left + 1, end);
    }
  }
}

let arr = [6, 5, 4, 1, 1, 1];

console.log("정렬 전 : ", arr);
qsort(arr, 0, arr.length - 1);
console.log("정렬 후 : ", arr);
반응형