Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is a REPL of the code. The idea here is to learn how the basic insertion sort algorithm works rather than to use existing sort libraries.

How can it be clearer and more efficient?

function insertionSort(array)
{
    for(var i = 1; i < array.length; ++i)
    {
        var currentItem = array[i];

        for(var j = i - 1; j >= 0; --j)
        {
            if(array[j] <= currentItem) break;
            array[j + 1] = array[j];
        }

        array[j + 1] = currentItem;
    }

    return array;
}

//test
var initialArray = [ 2, 4, 6, 8, 0, 5, 2, 5, 7, 8, 4, 7, 9 ];
var solutionArray = [ 0, 2, 2, 4, 4, 5, 5, 6, 7, 7, 8, 8, 9 ];

var sortedArray = insertionSort(initialArray);

var passed = JSON.stringify(sortedArray)==JSON.stringify(solutionArray);
console.log('passed:' + passed);
share|improve this question
3  
Welcome to Code Review! This is an incomplete post. Please add some more to the question other than code. What would you like reviewed about your code? – SirPython May 18 '16 at 19:25
    
@SirPython Hmm. I lack any particular questions other than wanting someone to review the code and to give his/her opinion. I think that this might be outside the scope of this StackExchange. Is that right? – Shaun Luttin May 18 '16 at 20:09
2  
Definitely not; your post is perfect here. I'm just saying questions with only code in the post aren't so well received. You could very well add "Any recommendations are welcome" at the bottom. And, you could even explain what the insertion sort is and how your code is accomplishing that. – SirPython May 18 '16 at 20:24
4  
Your code can be dramatically simplified by just writing array.sort() -> repl.it/CSTs/7 – Jonathan May 18 '16 at 20:39
    
@Jonathan Good call. Indeed that's true. :) I updated my question to emphasize my interest in learning algorithms. – Shaun Luttin May 18 '16 at 20:55

Eliminating some unnecessary code

Some programmers find break statements to be problematic. If you'd like to know more, here is a programmers stack exchange question about breaks and continues.

You could easily eliminate your use of break by just changing the inner loop to the following:

for (var j = i - 1; j >= 0 && array[j] > currentItem; j--) {
  array[j + 1] = array[j];
}

This shortens your code a bit and condenses all the control flow logic of you loop into one spot.


You could shorten your code even further by eliminating the conditional j >= 0:

for (var j = i - 1; array[j] > currentItem; j--) {
  array[j + 1] = array[j];
}

However, this might obfuscate the code a bit.

Note: This works because array[-1] will return undefined and undefined > myVar will always evaluate to false - ECMAScript Standard

share|improve this answer
    
What do some programmers find problematic about break statements? – Shaun Luttin May 19 '16 at 15:53
1  
I've added a link to my answer that goes into depth on using breaks and continues. I'd offer an explanation of my own but it would not be as thorough. – Josh Dawson May 19 '16 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.