- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.2k
Open
Description
merge-sort-inplace.js is not an example of an in place Merge Sort because arrays are being created and concatenated. Here's an example of an in place merge sort...
const mergeSort = (array, left = 0, right = array.length - 1) => {
    
    if (left >= right) return array
    let mid = Math.floor((left + right) / 2),
        i = mid + 1
    
    mergeSort(array, left, mid)
    mergeSort(array, i, right)
    while (left <= mid && i <= right) {
        if (array[left] < array[i]) left++
        else {
            array.splice(left, 0, array.splice(i, 1)[0])
            left++
            mid++
            i++
        }
    }
    return array
}Metadata
Metadata
Assignees
Labels
No labels