Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Squares of a Sorted Array
- function MergeSortedLists(x::Array{Int64, 1}, y::Array{Int64, 1})
- nx = length(x)
- ny = length(y)
- n = nx + ny
- z = Array{Int64}(undef, n)
- c = 1
- cx = 1
- cy = 1
- if x[1] < y[1]
- z[1] = x[1]
- cx += 1
- else
- z[1] = y[1]
- cy += 1
- end
- while c < n
- c += 1
- if x[cx] < y[cy]
- z[c] = x[cx]
- cx += 1
- if cx > nx
- c += 1
- z[c:end] = y[cy:end]
- c = n
- end
- else
- z[c] = y[cy]
- cy += 1
- if cy > ny
- c += 1
- z[c:end] = x[cx:end]
- c = n
- end
- end
- end
- return z
- end
- function main(z::Array{Int64, 1})
- x = z[z.<0] # list of negative numbers
- y = z[z.>=0] # list of positive numbers
- x .^= 2
- y .^= 2
- return MergeSortedLists(reverse(x), y)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement