Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: Use a range instead of a for loop on $i)
(→‎{{header|Javascript}}: An insertion sort should have the property that nearly sorted lists sort in linear time and the splice method didnt work that way.)
Line 656: Line 656:
function insertionSort (a) {
function insertionSort (a) {
for (var i = 0; i < a.length; i++) {
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < i; j++) {
var k = a[i];
if (a[i] < a[j]) {
for (var j = i; j > 0 && k < a[j - 1]; j--)
var k = a.splice(i, 1);
a[j] = a[j - 1];
a.splice(j, 0, k);
a[j] = k;
break;
}
}
}
}
return a;
return a;