开发者

JAVASCRIPT Is this a good function for simulation of the sort() method?

I wanted to make my own method for sorting an array high to low. Yes, I know that I can use sort(), and yes, I know I should use sort, but I'm a newbie programmer and wanted a little challenge.

So here's the live code for it: http://jsfiddle.net/qaaPu/3/

and here's the code:

//JavaScript sort() high to low simulation

//INPUT ARRAY:
var input = [1,2,3,4,5,6,7,8,9,10];

entireLoop: 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] &g开发者_如何学Ct; input[0]) {
                input.splice(0, 0, input[i]);
                input.splice(i + 1, 1);
            }
            else {
                if (input[o] > input[i]) {
                    input.splice(o + 1, 0, input[i]);
                    input.splice(i + 1, 1);
                    continue entireLoop;
                }
            }
        }
    }
}
document.body.innerHTML = (input);

I'm just wondering if this is a good solution, or if there's a problem with how I did it (hogging RAM, or very hard on the processor, bandwidth, etc.)

So what do you pros think of this code?


for further inspiration watch that link: http://phpjs.org/functions/sort:519

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜