Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Ring}}: Remove vanity tags) |
|||
Line 666: | Line 666: | ||
Example: |
Example: |
||
[1,3,5,2,4,6] | strand_sort |
[1,3,5,2,4,6] | strand_sort |
||
=={{header|Julia}}== |
|||
{{trans|Python}} |
|||
<lang julia>function mergelist(a, b) |
|||
out = Vector{Int}() |
|||
while !isempty(a) && !isempty(b) |
|||
if a[1] < b[1] |
|||
push!(out, popfirst!(a)) |
|||
else |
|||
push!(out, popfirst!(b)) |
|||
end |
|||
end |
|||
append!(out, a) |
|||
append!(out, b) |
|||
out |
|||
end |
|||
function strand(a) |
|||
i, s = 1, [popfirst!(a)] |
|||
while i < length(a) + 1 |
|||
if a[i] > s[end] |
|||
append!(s, splice!(a, i)) |
|||
else |
|||
i += 1 |
|||
end |
|||
end |
|||
s |
|||
end |
|||
strandsort(a) = (out = strand(a); while !isempty(a) out = mergelist(out, strand(a)) end; out) |
|||
println(strandsort([1, 6, 3, 2, 1, 7, 5, 3])) |
|||
</lang>{{output}}<pre> |
|||
[1, 1, 2, 3, 3, 5, 6, 7] |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |