Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:39:34+00:00 2026-05-23T00:39:34+00:00

I need a recommendation for a good implementation of merge sort in Java. Basically,

  • 0

I need a recommendation for a good implementation of merge sort in Java.
Basically, I can write all the merges but if I would have a good jar from a provider such as Apache or Google it would be nicer.

Requirements for the merge sort:

  1. I get an unknown number of sorted arrays.
  2. Very important: I get a number that says when to stop (lets say it says 34 on a random incident – I want my algorithm to stop sorting when it reaches that number).

No need to write the code here, I’m only looking for an available jar/library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:39:35+00:00Added an answer on May 23, 2026 at 12:39 am

    I don’t know of a ready-made solution, but it appears simple enough to implement with the help of a priority queue:

    import static java.util.Arrays.asList;
    
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
    import java.util.PriorityQueue;
    
    public class MergingIterator<T> implements Iterator<T> {
    
        public static class InputIter<T> {
            final Iterator<T> source;
            T data;
    
            public InputIter(Iterable<T> list) {
                source = list.iterator();
                read();
            }
    
            public void read() {
                if (source.hasNext()) {
                    data = source.next();
                } else {
                    data = null;
                }
            }
        }
    
        final PriorityQueue<InputIter<T>> queue;
    
        public MergingIterator(final Comparator<? super T> cmp, Iterable<T>... lists) {
            queue = new PriorityQueue<InputIter<T>>(lists.length, new Comparator<InputIter<T>>() {
                @Override
                public int compare(InputIter<T> o1, InputIter<T> o2) {
                    return cmp.compare(o1.data, o2.data);
                }
            });
            for (Iterable<T> list : lists) {
                InputIter<T> ii = new InputIter<T>(list);
                if (ii.data != null) {
                    queue.add(ii);
                }
            }
        }
    
        @Override
        public boolean hasNext() {
            return !queue.isEmpty();
        }
    
        @Override
        public T next() {
            InputIter<T> ii = queue.poll();
            T next = ii.data;
            ii.read();
            if (ii.data != null) {
                queue.add(ii);
            }
            return next;
        }
    
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
    

    Test code:

        Comparator<Integer> cmp = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        };
        List<Integer> empty = asList();
        Iterator<Integer> iter = new MergingIterator<Integer> (
            cmp,
            asList(1, 2, 4, 5, 7, 8),
            asList(3),
            asList(6, 9),
            asList(0),
            empty
        );
    
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    

    The space complexity is O(L), and the time complexity is O((L + K) log(L)) where L is the number of lists, and K the number of retrieved elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java desktop application I wrote, and I would like a recommendation
I need a recommendation for a good embeddable Java application server that is small,
I need a recommendation for a pythonic library that can marshall python objects to
I have asked this question countless times on various forums. Ultimately all I need
Is there a good tool that can help to reverse engineer Java classes to
I need recommendations for good application servers for monitoring and debugging PHP and Mysql
I need recommendations on a good Unit Testing book for use with ASP.NET MVC.
I need recommendations for a good (free) development notepad to work in on my
Recently I need to make a chart of employees' workload.Any recommendation? Thanks.
I need recommendations from people whom have implemented an easy to use GWT editable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.