sorting algorithms in node.js
function sort(arr, compareFn = (a, b) => a <= b) {
if (!arr instanceof Array || arr.length === 0) {
return arr;
}
if (typeof compareFn !== 'function') {
throw new Error('compareFn is not a function!');
}
const partition = (arr, low, high) => {
const pivot = arr[low];
while (low < high) {
while (low < high && compareFn(pivot, arr[high])) {
--high;
}
arr[low] = arr[high];
while (low < high && compareFn(arr[low], pivot)) {
++low;
}
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
};
const quickSort = (arr, low, high) => {
if (low < high) {
let pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
return arr;
};
return quickSort(arr, 0, arr.length - 1);
}
Also in JavaScript:
- loop through object jquery
- react a
- pass element from child to parent react
- javascript conver time into 24 hour format
- js copy a div
- js self driving car
- js focus event
- error Unexpected end of JSON input while parsing near '...
- saveas angular 6
- function range() as range js
- mongoose save return id
- dropdown validation using jquery
- how to import npm packages in node.js?
- several node versions
- react dom
- query string from object js
- check array for duplicate values javascript
- JavaScript and HTML DOM Reference
- discord.js clear console
- jquery count elements
- javascript clear array
- '@babel/plugin-proposal-class-properties' webpack
- new date() in javascript 3 days from now
- jquery number format comma