Jump to content

Sorting algorithms/Gnome sort: Difference between revisions

No edit summary
Line 1,380:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<lang Juliajulia>function gnomesort!(Aarr::AbstractVectorVector)
Julia is one-indexed so the implementation differs slightly from the pseudocode.
i, j = 1, 2
 
while i < length(Aarr)
<lang Julia>function gnomesort(A::AbstractVector)
if arr[i] = arr[i+1]
j = 2
while i < length(A)
if A[i] <= A[i + 1]
i = j
j += 1
else
Aarr[i], Aarr[i + 1] = Aarr[i + 1], Aarr[i]
i -= 1
if i == 0
Line 1,399 ⟶ 1,397:
end
end
Areturn arr
end
 
v = rand(-10:10, 10)
A = randcycle(20)
println("unsorted# unordered: $v\n -> ordered: ", Abogosort!(v))</lang>
println("sorted: ", gnomesort(A))</lang>
 
{{out}}
<pre># unordered: [9, -8, 0, 3, 2, 10, 5, 4, 5, 5]
 
<pre ->unsorted ordered: [13-8,15 0,6,11 2, 3,19 4,17 5,20 5,2,18 5, 9,14,8, 10,16,7,12,1,4,5]</pre>
sorted: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]</pre>
 
=={{header|Kotlin}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.