Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
Line 2,145: Line 2,145:


=={{header|Julia}}==
=={{header|Julia}}==
<lang Julia>
{{works with|Julia|0.6}}
function bubblesort!{T}(x::AbstractArray{T})


<lang julia>function bubblesort!(arr::AbstractVector)
for i in 2:length(x)
for j in 1:length(x)-1
for _ in 2:length(arr), j in 1:length(arr)-1
if x[j] > x[j+1]
if arr[j] > arr[j+1]
tmp = x[j]
arr[j], arr[j+1] = arr[j+1], arr[j]
x[j] = x[j+1]
end
x[j+1] = tmp
end
end
end
return arr
end

return x
end
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}}
{{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}}==
=={{header|Kotlin}}==