Question
Test Case 1
Shell
복사
Test Case 2
Shell
복사
Solve
const quickSort = function (arr, func = (num) => num, left = 0, right = arr.length - 1) {
if(left < right){
const mid = Math.floor((left+right)/2);
const pivot = arr[mid];
const partition = divide(arr, left, right, pivot, func);
quickSort(arr, func, left, partition-1);
quickSort(arr, func, partition, right);
}
return arr;
};
const divide = (arr, left, right, pivot, func) => {
while(left <= right){
while(func(arr[left]) < func(pivot)){
left++;
}
while(func(arr[right]) > func(pivot)){
right--;
}
if(left <= right){
let temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
return left;
};
JavaScript
복사
실행시간 : ms