Make `Iterators.mergeSorted()` stable
See original GitHub issueI’ve noticed that Iterators.mergeSorted()
is unstable.
So for example if I have arrays: [A_1, B_1, C_1]
[A_2, B_2, C_2]
(where the comparator looks only at the letter), I’d like the result to iterate [A_1, A_2, B_1, B_2, C_1, C_2]
. The current implementation doesn’t guarantee this.
Might it be possible to add a stable version?
I confess it’s not obvious what to do if the arrays are something like [A_1, A_2, B_1]
, [A_3]
- to me it would make sense to empty the first iterator of A_n
elements before moving on to the second, so [A_1, A_2, A_3, B_1]
.
[I appreciate I’m being a little lazy with my terminology here - I can provide a fully fleshed example if it’s not clear]
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Merge Sort using iterators in C++ - Stack Overflow
Problem is here merge(result, vector, vector.begin(), mid); merge(result, vector, mid, vector.end());. That should be
Read more >c++ - Merge Sort implemented using iterators
This seems like a straightforward top-down implementation of mergesort. However, there are some issues: Stability.
Read more >https://www.cs.uaf.edu/2009/fall/cs311/docs/merge_...
Merge is done in a stable manner. // Requirements on Types: // FDIter is a forward iterator type. // operator< is a total...
Read more >Google | Onsite | Merge K Sorted Iterators - LeetCode Discuss
Standard PQ solution for merging K sorted lists, clean. constructor - O(K*logK) - where K - number of lists(iterators) hasNext() - O(1) speed...
Read more >stable_sort() in C++ STL - GeeksforGeeks
first: iterator pointing to the first element in the range. last: iterator pointing to ... C++ program to demonstrate stable sort() in STL....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, I am new to open source contribution projects. I see that this issue is still open. I was hoping that I could work on it if it has not already been implemented?
@chaoren / google folk: I’ve had a go at solving this, see https://gist.github.com/msmerc/fff91f4049dd4d8e39f3614bb0c58acb
My solution is to generate a comparable such that objects are equal (using the original comparator) it will compare the position in the initial array. Is this something you’d want in the main library? Let me know.