Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,42 @@
|
|||
function mergesort!(A::AbstractVector)
|
||||
if length(A) <= 1
|
||||
return A
|
||||
end
|
||||
middle = div(length(A), 2)
|
||||
left = mergesort(A[1:middle])
|
||||
right = mergesort(A[middle + 1:end])
|
||||
result = Array(eltype(left), length(left) + length(right))
|
||||
idx = 1
|
||||
@inbounds while !isempty(left) && !isempty(right)
|
||||
if left[1] <= right[1]
|
||||
result[idx] = left[1]
|
||||
left = left[2:end]
|
||||
else
|
||||
result[idx] = right[1]
|
||||
right = right[2:end]
|
||||
end
|
||||
idx += 1
|
||||
end
|
||||
@inbounds while !isempty(left)
|
||||
result[idx] = left[1]
|
||||
left = left[2:end]
|
||||
idx += 1
|
||||
end
|
||||
@inbounds while !isempty(right)
|
||||
result[idx] = right[1]
|
||||
right = right[2:end]
|
||||
idx += 1
|
||||
end
|
||||
for i=1:length(A)
|
||||
A[i] = result[i]
|
||||
end
|
||||
return A
|
||||
end
|
||||
|
||||
function mergesort(A::AbstractVector)
|
||||
return mergesort!(copy(A))
|
||||
end
|
||||
|
||||
A = randcycle(10)
|
||||
println("unsorted: ", A)
|
||||
println("sorted: ", mergesort(A))
|
||||
Loading…
Add table
Add a link
Reference in a new issue