0

Here's the code I wrote:

function mergeSort(array){
    if(array.length < 2) return array;
    var mid = Math.floor(array.length / 2);
    var left = array.slice(0, mid);
    var right = array.slice(mid, array.length);
    return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right){
    var result = [];
    while (left.length && right.length){
        if(left[0]>right[0]){
            result.push(right[0]);
        } else {
            result.push(left[0]);
        }

    }
    while(left.length){
        result.push(left[0]);
    }
    while(right.length){
        result.push(right[0]);
    }
    return result;
}
array = [1000, -94, -115, 300, 22]
mergeSort(array);

and below is another solution i found online

    function mergeSort (arr) {
    if (arr.length < 2) return arr;

    var mid = Math.floor(arr.length /2);

    return merge(mergeSort(arr.slice(0,mid)),  mergeSort(arr.slice(mid)));
}

function merge (a,b) {
    var result = [];
    while (a.length >0 && b.length >0)
        result.push(a[0] < b[0]? a.shift() : b.shift());
    return result.concat(a.length? a : b);
}

var test = [-100,3,53,21,4,0];
console.log(mergeSort(test));

in comparison I can't find any significant difference besides some syntax. But for some reason mine code won't run in both chrome dev console and node.js environment. In chrome, it won't return any result and in node.js it gives me

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory Abort trap: 6 Can someone help me understand what is the difference between the two snippet that actually made the difference?

Thanks in advance!

2 Answers 2

2

Think about it, you have an array left and you do

while(left.length){
    result.push(left[0]);
}

left[0] doesn't change the array, it just gets the first item.

The length of left will never change, what you have there is a while loop that goes on as long as the array has a length over zero, and it always will, as the length never changes.
This is a perfect example of an infinite loop, that eventually fills the callstack and errors out, or in older browsers just crashes.

However if you do

while(left.length){
    result.push(left.shift());
}

Array.shift() removes the first item from the array, so at some point the arrays length will be zero, and the loop stops

Sign up to request clarification or add additional context in comments.

Comments

1

You never move from first element when you are doing merge. Try this code:

function mergeSort(array){
    if(array.length < 2) return array;
    var mid = Math.floor(array.length / 2);
    var a = array.slice(0, mid);
    var b = array.slice(mid);
    return merge(mergeSort(a), mergeSort(b));
}

function merge(a, b){
    var result = [];
    var i = 0;
    var j = 0;
    while (i < a.length && j < b.length){
        if(a[i] < b[j]){
            result.push(a[i]);
            i++;
        } else {
            result.push(b[j]);
            j++;
        }
    }
    while(i < a.length){
        result.push(a[i]);
        i++;
    }
    while(j < b.length){
        result.push(b[j]);
        j++
    }

    return result;
}

array = [1000, -94, -115, 300, 22];
array = mergeSort(array);
console.log(array);

2 Comments

@SamHe If you can check answer as correct it would be really helpful :)
@ Luka Lopusina ahh, sorry I'm kinda new. Thanks for the reminder!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.