Forum Moderators: open

Message Too Old, No Replies

Sorting array

         

gordon1221

4:57 pm on Dec 5, 2014 (gmt 0)

10+ Year Member



I am new to JavaScript and I am having some issues figuring this out. The goal is to split the array into 2 arrays at the middle index and returns a left and right arrays that are sorted. The merge function then is called to combine the arrays back together in a sorted array. I am new at this and trying to understand it. Thank you

Here is my code:

"use strict";



var rArray = [2,1,4,3];
document.writeln("randomArray: " + rArray + "<br />");
var sortedArray = sort(rArray);
document.writeln("sortedArray: " + sortedArray + "<br />");




function merge(arr1, arr2){

var combine= [];
vararr1Len= arr1.length;
vararr2Len= arr2.length;
varx = 0, y = 0, add = 0;

while(x < arr1Len && y < arr2Len){
while(arr1[x] < arr2[y]){
combine[add++]= arr1[x++];
}

while(arr2[y] <= arr1[x]){
combine[add++]= arr2[y++];
}
}
if(x < arr1Len)
combine.splice(add, 0, arr1.slice(x));
else
if(y < arr2Len)
combine.splice(add, 0, arr2.slice(y));

document.writeln("arr1 has: [" + arr1 + "]</br>");
document.writeln("arr2 has: [" + arr2 + "]</br>");
document.writeln("arr3 has: [" + combine + "]");
return combine;
}



function sort(array){
var result;
if (array.length <= 1) {
document.writeln("At 1: " + array + "<br />");
return array
} else {
// divide the array in half, sort them separately
// and then merge them back together
var mid = (array.length / 2);
var left = array.slice(0,mid);
var end = (array.length - mid);
var right = array.slice(mid);
sort(left);
sort(right);
result = merge(left, right);

document.writeln("Output results: " + result + "<br />");
}
return result
}



This is the current output that I am getting:

randomArray: 2,1,4,3
At 1: 2
At 1: 1
arr1 has: [2]
arr2 has: [1]
arr3 has: [1,2] Output results: 1,2
At 1: 4
At 1: 3
arr1 has: [4]
arr2 has: [3]
arr3 has: [3,4] Output results: 3,4
arr1 has: [2,1]
arr2 has: [4,3]
arr3 has: [2,1,4,3] Output results: 2,1,4,3
sortedArray: 2,1,4,3

Fotiman

5:56 pm on Dec 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
First, I'm getting different results than you with that code. Here's what I see when I run that code:

randomArray: 2,1,4,3
At 1: 2
At 1: 1

So lets dive in.

Your sort method returns an array. However, where you're calling sort from within itself, you're never capturing the return value.

sort(left);
sort(right);
result = merge(left, right);

Next, your merge function is missing some spaces between the keyword "var" and the variables:

vararr1Len= arr1.length;
vararr2Len= arr2.length;
varx = 0, y = 0, add = 0;

As a result, these are created as globals, so each call to this function trashes the global variables (so the data you think you have may not be correct). Fix that by adding spaces:

var arr1Len= arr1.length;
var arr2Len= arr2.length;
var x = 0, y = 0, add = 0;


In my limited testing, fixing those two things caused the correct output:
sortedArray: 1,2,3,4

gordon1221

6:50 pm on Dec 5, 2014 (gmt 0)

10+ Year Member



Thank you for your reply. I fixed the issue with the variables having no spaces. However, I am not understanding the part about not capturing the return value.

Fotiman

8:22 pm on Dec 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, probably not needed because arrays would be passed by reference (as opposed to pass by value). I was thinking that your code might do something like this:

// left = [2,1]
sort(left); // return result of this would be [1,2]
// left still = [2,1]

However, because the sort/merge methods modify the passed in array, and because they are passed by reference, the original array gets changed so you end up with this:

// left = [2,1]
sort(left); // return result of this would be [1,2]
// left = [1,2]

I thought of that after I posted it, but didn't have time to test it. I figured it was probably just the space issue. :)

gordon1221

8:28 pm on Dec 5, 2014 (gmt 0)

10+ Year Member



Thank you for your assistance. It is working correctly now. ;)