peterschussheim
11/8/2016 - 10:50 PM

insertionSort

insertionSort

function insertionSort (a) {
    for (var i = 0; i < a.length; i++) {
        var k = a[i];
        for (var j = i; j > 0 && k < a[j - 1]; j--)
            a[j] = a[j - 1];
        a[j] = k;
    }
    return a;
}
 
var a = [4, 65, 2, -31, 0, 99, 83, 782, 1];
insertionSort(a);
document.write(a.join(" "));

insertionSort

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.