开发者

Can someone tell me why this JavaScript code isn't lining up an array in order?

Live code: http://jsfiddle.net/fCUZC/

开发者_如何学Go//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
entireLoop:
for (var i = 1; i<=input.length; i++)
{
    if(input[i] > input[i-1])
    {
        for(var o = i; o>=0; o--)
        {
            if(input[i-1] > input[o])
            {
                input.splice(i,0,input[o]);
                input.splice((o+1),1);
                continue entireLoop;
            }
            else if(input[o] > input[0])
            {
                input.splice(0,0,input[o]);
                input.splice((o+1),1);
                continue entireLoop;
            }

        }
    }
}
document.write(input);

I'm trying to order the array from largest to smallest, but there's a 32 stuck somewhere. I know there's the sort method, but I'm a newbie and want to try this for myself.


** edit ** First have a look at the Array's native .sort() method. It leaves the original array intact and accepts a comparison function. The latter makes .sort() pretty powerful.

var input = [28,32,21,11,8,2,14,32,64];

var low2high = function ( a , b ) {
    return a > b;
};

var high2low = function ( a , b ) {
    return a < b;
};

var resultHigh2low = input.sort( high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
var resultLow2high = input.sort( low2high ); // [ 2,8,11,14,21,28,32,32,64 ];

So if we want to use bubbleSort ( link provided by T.J. Crowder , see OP comments ) we can write the following:

// javascript bubbleSort implementation
var bubbleSort = function ( list , comparison ) {
    var swapped;
    var i;
    var val;

    list = [].concat( list ); // do not destroy original
    comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}

    do {
        i = list.length;
        while ( --i ) {
           if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
                val = list[ i ];
                list[ i ] = list[ i - 1 ];
                list[ i - 1] = val;
                swapped = true;
           }
        }
    } while ( swapped );
    return list;
}

// using comparison functions from previous example.
var resultHigh2low = bubbleSort( input , high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
var resultLow2high = bubbleSort( input , low2high ); // [ 2,8,11,14,21,28,32,32,64 ];

Lets walk through it step by step:

var bubbleSort = function ( list , comparison ) {
    ..code..
}

Our function accepts 2 parameters, first the array and 2nd an optional comparison function.

var swapped;
var i = list.length;
var val;

We store the list's length under variable i, and declare 2 empty variables ( swapped and val ) we're going to use later on.

list = [].concat( list ); // do not destroy original

We clone the list using [].concat( array ) and overwrite the local list variable leaving the original intact.

comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}

We test the typeof the comparison argument, if it's a function we use that one, otherwise we fall back on our own comparison function. Our fallback comparison function will return true if a is bigger than b.

do {
    ..code..
} while ( swapped );

A do/while loop will run at least once, our swapped variable is currently undefined so it will be interpreted as falsy. If our comparison function returns true, a swap occurs and the swapped variable will be set to true, so it will loop again.

while ( --i ) {
    ..code..
}

Here I loop from the list's length downward, the -- operator is put before the i variable to ensure it is handled first before anything, i-- would go off after while evaluation causing erronous results since list[ list.length ] does not exist. I always do it this way (bad habbit perhaps), but if it confuses you, go for absolute transparancy.

if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
    ..code..
}

First we check if i has a truthy value ( 0 evaluates to falsy ) and then we run the comparison function passing list[ i ] and list[ i - 1 ] as a and b parameters. If the comparison function returns true, we perform a swap.

val = list[ i ];
list[ i ] = list[ i - 1 ];
list[ i - 1] = val;
swapped = true;

Here I perform the swap without using the .splice() method, it's just an educated guess atm., but I figure direct assignments are faster then function calls. I use the val variable as a place holder. After the swap is done, I set swapped to true so our do/while loop will continue.

return list;

Well... return the result.

I've excluded some checks, like what do we do when the list's length is 0 and whatnot. Basically when writing helper functions, we also need to deal with error handling. Like for example throwing a TypeError when the passed comparison argument is not a function, ensuring the comparison method returns a boolean value and so on.


//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
for (var i = 1; i<input.length; i++)
{
    if(input[i] > input[i-1])
    {
        for(var o = i-1; o>=0; o--)
        {
            if(input[i] > input[o])
            {
                input.splice(i+1,0,input[o]);
                input.splice((o),1);
                i--;
            }       
        }
    }
}
document.write(input);

While it's still not great, it should work. Keep in mind I barely tested this and I'm fairly inexperienced with javascript. Your intentions weren't all bad and everyone needs to start somewhere.

The biggest issue was simply the inner conditional. I can see your logic of looping backwards from the large value you've found and pushing all smaller values to the right. Unfortunately your indexes are a little off. You also need to run this loop until it finishes normally instead of continuing, else you'll only switch one value. When this conditional is fixed the second one is no longer needed

The alternate form would have been to start from the lowest index and find the first value that is smaller than input[i] and place it there. This is potentially clearer.

I think this was a pretty good first shot and wasn't that hard to get working. Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜