-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add algorithm.merge #16182
add algorithm.merge #16182
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have an opinion on whether this proc is worth adding, but I'd suggest that the implementation should avoid newSeqOfCap
. I made a quick code suggestion that uses newSeq
instead.
That gives a speedup of roughly 5x (with -d:danger --passC:-flto
) when merging two sorted arrays each containing 1000 random integers in 0..10_000
:
Implementation | Elapsed time |
---|---|
Using newSeqOfCap |
11.59 ± 0.37 μs |
Using newSeq |
2.45 ± 0.21 μs |
(And roughly 3x for two sorted arrays each containing 100 random integers in 0..1000
). See this gist.
And can we avoid snake_case
for identifiers? Currently this PR has:
size_x
size_y
index_x
index_y
item_x
item_y
@ee7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issorted
-> isSorted
Co-authored-by: ee7 <45465154+ee7@users.noreply.github.com>
@ee7 |
Given that this proc returns a new seq, is The style guide wording suggests that we should call it
Lines 334 to 339 in 3589631
Also, the Nim 1.2.0 release blog post contained:
Is that relevant here? |
It seems that inplace version is much slower(N * M) than the version with extra space(N + M) or complex(best performance needs quick sort, (N+M) * log(N+M)). |
indeed. also noticed in #13448. It's so bad that |
Should I implement inplace version(though it looks ugly to me) instead of outplace version? |
sugar.dup (or if I revive it, #13309 which has better syntax) obsoletes the need for duplicate pairs of inplace+outplace versions. This gives us N API's instead of 2N APIs. |
All right, I will implement the inplace version soon. |
we need to decide on #16182 (comment) before merging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
assert i == 0 or cmp(a, result[i-1]) <= 0 # or similar
result[i] = a (can use a template since used in a few locations) |
@Araq friendly ping on this |
* add merge to algorithm * Apply suggestions from code review * Update lib/pure/algorithm.nim * Apply suggestions from code review * Update changelog.md Co-authored-by: ee7 <45465154+ee7@users.noreply.github.com>
* fix nim js cmp fails at CT * followup nim-lang#16182
Merge two sorted array:
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
followup:
see #17472