Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
Line 2,145:
 
=={{header|Julia}}==
<lang{{works with|Julia>|0.6}}
function bubblesort!{T}(x::AbstractArray{T})
 
<lang julia>function bubblesort!{T}(xarr::AbstractArray{T}AbstractVector)
for i in 2:length(x)
for _ in 2:length(arr), j in 1:length(xarr)-1
if xarr[j] > xarr[j+1]
tmp arr[j], arr[j+1] = xarr[j+1], arr[j]
x[j] = x[j+1]end
x[j+1] = tmp
end
end
return xarr
end
 
return x
end
 
v = rand(-10:10, 10)
 
println("# unordered: $v\n -> ordered: ", bubblesort!(v))</lang>
a = [rand(-100:100) for i in 1:20]
println("Before bubblesort:")
println(a)
a = bubblesort!(a)
println("\nAfter bubblesort:")
println(a)
</lang>
 
{{out}}
<pre># unordered: [7, 4, -1, -8, 8, -1, 5, 6, -3, -5]
<pre>
-> ordered: [-8, -5, -3, -1, -1, 4, 5, 6, 7, 8]</pre>
Before bubblesort:
[95,-40,-93,38,95,-20,-13,-61,81,51,-54,77,-4,-49,-99,-55,28,-52,2,-28]
 
After bubblesort:
[-99,-93,-61,-55,-54,-52,-49,-40,-28,-20,-13,-4,2,28,38,51,77,81,95,95]
</pre>
 
Here <code>bubblesort</code> was used on a list of integers. As written the function will work on lists of any objects for which <code>isless</code> is defined.
 
=={{header|Kotlin}}==