Sorting algorithms/Counting sort: Difference between revisions

Added a V implementation
No edit summary
(Added a V implementation)
Line 2,606:
-8, -6, -4, -2, 1, 3, 5, 7, 11, 100, 300
</pre>
 
=={{header|Vlang}}==
<lang vlang>fn counting_sort(arr mut []int, min int, max int) {
println('Input: ' + arr.str())
mut count := [0].repeat(max - min + 1)
for i in 0 .. arr.len {
nbr := arr[i]
ndx1 := nbr - min
count[ndx1] = count[ndx1] + 1
}
mut z := 0
for i in min .. max {
curr := i - min
for count[curr] > 0 {
arr[z] = i
z++
count[curr]--
}
}
println('Output: ' + arr.str())
}
 
fn main() {
mut arr := [6, 2, 1, 7, 6, 8]
counting_sort(mut arr, 1, 8)
}</lang>
{{out}}
<pre>Input: [6, 2, 1, 7, 6, 8]
Output: [1, 2, 6, 6, 7, 8]</pre>
 
=={{header|XPL0}}==
Anonymous user