Anonymous user
Sorting algorithms/Insertion sort: Difference between revisions
Sorting algorithms/Insertion sort (view source)
Revision as of 13:44, 1 September 2009
, 14 years agoAdded R code
(Added J program) |
(Added R code) |
||
Line 404:
# insert key at position ``low``
seq[:] = seq[:low] + [key] + seq[low:i] + seq[i + 1:]</lang>
=={{header|R}}==
Direct translation of pseudocode.
<lang r>
insertionsort <- function(x)
{
for(i in 2:(length(x)))
{
value <- x[i]
j <- i - 1
while(j >= 1 && x[j] > value)
{
x[j+1] <- x[j]
j <- j-1
}
x[j+1] <- value
}
x
}
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782
</lang>
=={{header|REALbasic}}==
|