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++) { |
||
var k = a[i]; |
|||
for (var j = i; j > 0 && k < a[j - 1]; j--) |
|||
a[j] = a[j - 1]; |
|||
a[j] = k; |
|||
break; |
|||
} |
|||
} |
|||
} |
} |
||
return a; |
return a; |