array sorting javascript mergesort
// Merge Sort (Recursive)
function mergeSort (unsortedArray) {
// No need to sort the array if the array only has one element or empty
if (unsortedArray.length <= 1) {
return unsortedArray;
}
// In order to divide the array in half, we need to figure out the middle
const middle = Math.floor(unsortedArray.length / 2);
// This is where we will be dividing the array into left and right
const left = unsortedArray.slice(0, middle);
const right = unsortedArray.slice(middle);
// Using recursion to combine the left and right
return merge(
mergeSort(left), mergeSort(right)
);
}
Also in JavaScript:
- how to add all values of array together js
- function in react
- testing library react hooks
- add property to object javascript
- javascript does not equal
- javascript json deserialize
- javascript string replace
- recursive permutation
- javacript is checkbox checked
- call local function javascript
- javascript project ideas
- permission api js
- js add event listener
- int to string javascript
- inline math mathjax
- js query string
- how to create my own filter in js
- javascript test for empty object
- bootstrap in javascript
- indexing string in javascript
- change the value in checkbox by button react
- react native vs flutter
- 11. Which of the following metals catch fire on reaction with air? A. Magnesium B. Manganese C. Potassium D. Calcium
- javascript switch case regex