Sorting algorithms/Insertion sort: Difference between revisions

Added a V implementation
(Added a V implementation)
Line 3,522:
<pre>ORIGINAL : 26699;2643;10249;31612;21346;19702;29799;31115;20413;5197;
SORTED : 2643;5197;10249;19702;20413;21346;26699;29799;31115;31612;</pre>
 
=={{header|Vlang}}==
<lang vlang>fn insertion(arr mut []int) {
for i in 1 .. arr.len {
value := arr[i]
mut j := i - 1
for j >= 0 && arr[j] > value {
arr[j + 1] = arr[j]
j--
}
arr[j + 1] = value
}
}
 
fn main() {
mut arr := [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
println('Input: ' + arr.str())
insertion(mut arr)
println('Output: ' + arr.str())
}</lang>
{{out}}
<pre>Input: [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
Output: [-31, 0, 1, 2, 2, 4, 65, 83, 99, 782]</pre>
 
=={{header|XPL0}}==
Anonymous user