You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import algorithm
procmerge*[T](x, y: openArray[T], cmp: proc(x, y: T): int {.closure.}, order =SortOrder.Ascending): seq[T] =## Merges two sorted `openArray`. All of inputs are assumed to be sorted.## If you do not wish to provide your own ``cmp``,## you may use `system.cmp` or instead call the overloaded## version of `merge`, which uses `system.cmp`.#### **See also:**## * `merge proc<#merge,openArray[T],openArray[T]>`_
runnableExamples:
import algorithm
let x =@[1, 3, 6]
let y =@[2, 3, 4]
let res = x.merge(y, system.cmp[int])
assert res.isSorted
assert res ==@[1, 2, 3, 3, 4, 6]
let
size_x = x.len
size_y = y.len
result=newSeqOfCap[T](size_x + size_y)
var
index_x =0
index_y =0whiletrue:
if index_x == size_x:
result.add y[index_y ..^1]
returnif index_y == size_y:
result.add x[index_x ..^1]
returnlet item_x = x[index_x]
let item_y = y[index_y]
ifcmp(item_x, item_y) * order >0:
result.add item_y
inc index_y
else:
result.add item_x
inc index_x
Merge two sorted array
Function Prototype:
C++ (merge algorithm):
http://www.cplusplus.com/reference/algorithm/merge
D:
https://dlang.org/library/std/algorithm/sorting/merge.html
Python(based on heap)
https://docs.python.org/3/library/heapq.html#heapq.merge
The text was updated successfully, but these errors were encountered: