Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Computer Science Introduction to Data Structures The Merge Sort Algorithm Ensuring the Correctness of Merge Sort

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Can somebody please help me understand this JS code

I did not understand the code Pasan Wrote and then I checked Some with Js example. But I am not able to figure out , how and from where the control is going to which function. When the Merge Function is finished, how the control is switching to Merge Sort function without calling again. Basically, after the first Merge Function, how the MergeSort is being executed again ? with mergeSort(arr, m+1, r )?

function merge(arr, l, m, r)
{
    console.log(`L = ${l}, m = ${m} and r = ${r}`)
    var n1 = m - l + 1;
    var n2 = r - m;

    // Create temp arrays
    var L = new Array(n1);
    var R = new Array(n2);

    // Copy data to temp arrays L[] and R[]
    for (var i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (var j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];


    // Merge the temp arrays back into arr[l..r]

    // Initial index of first subarray
    var i = 0;

    // Initial index of second subarray
    var j = 0;

    // Initial index of merged subarray
    var k = l;

    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];  
            j++;
        }
        k++;
    }

    // Copy the remaining elements of
    // L[], if there are any
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    // Copy the remaining elements of
    // R[], if there are any
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

// l is for left index and r is
// right index of the sub-array
// of arr to be sorted */
function mergeSort(arr,l, r){
    if(l>=r){
        return;//returns recursively
    }
    var m =l+ parseInt((r-l)/2);
    mergeSort(arr,l,m);
    mergeSort(arr,m+1,r);
    merge(arr,l,m,r);
}

// UTILITY FUNCTIONS
// Function to print an array
function printArray( A, size)
{
    for (var i = 0; i < size; i++)
    document.write( A[i] + " ");
}


var arr = [ 12, 11, 13, 5, 6, 7 ];
    var arr_size = arr.length;

    document.write( "Given array is <br>");
    printArray(arr, arr_size);

    mergeSort(arr, 0, arr_size - 1);

    document.write( "<br>Sorted array is <br>");
    printArray(arr, arr_size);

1 Answer

Steven Parker
Steven Parker
231,271 Points

It looks like you're a techdegree student, and if I remember correctly that gives you access to a special Slack channel where you can chat live with a student success specialist. This might be a perfect time to make use of that resource.