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

  • SEARCH
  • Home
  • 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 248379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:20:17+00:00 2026-05-11T21:20:17+00:00

I have 8 sorted lists that I need to merge into 1 sorted list.

  • 0

I have 8 sorted lists that I need to merge into 1 sorted list. I don’t know the best way to do this. I was thinking of the following:

void merge_lists_inplace(list<int>& l1, const list<int>& l2)
{
    list<int>::iterator end_it = l1.end();
    --end_it;
    copy(l2.begin(), l2.end(), back_inserter(l1));
    ++end_it;
    inplace_merge(l1.begin(), end_it, l1.end());
}

list<int> merge_8_lists(list<int>[8] lists)
{
    merge_lists_inplace(lists[0], lists[1]);
    merge_lists_inplace(lists[2], lists[3]);
    merge_lists_inplace(lists[4], lists[5]);
    merge_lists_inplace(lists[6], lists[7]);

    merge_lists_inplace(lists[0], lists[2]);
    merge_lists_inplace(lists[4], lists[6]);

    merge_lists_inplace(lists[0], lists[4]);

    return lists[0];
}

But would it be better to just worry about the sorting last?

list<int> merge_8_lists(list<int>[8] lists)
{
    for (int i = 1; i < 8; ++i)
        copy(lists[i].begin(), lists[i].end(), back_inserter(lists[0]));        
    lists[0].sort();
    return lists[0];
}

Side note: I don’t care that the lists are modified.

  • 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-11T21:20:17+00:00Added an answer on May 11, 2026 at 9:20 pm

    A simple extension of merge sort’s merge phase can do this in O(n lg m) time (where n = total number of items and m = number of lists), using a priority queue (eg, a heap). Pseudocode:

    Let P = a priority queue of the sorted lists, sorted by the smallest element in each list
    Let O = an empty output list
    While P is not empty:
      Let L = remove the minimum element from P
      Remove the first element from L and add it to O
      If L is not empty, add L to P
    

    And a simple (untested!) concrete implementation in C++:

    #include <list>
    #include <set>
    
    template<typename T>
    struct cmp_list {
        bool operator()(const std::list<T> *a, const std::list<T> *b) const {
            return a->front() < b->front();
        }
    };
    
    template<typename T>
    void merge_sorted_lists(std::list<T> &output, std::list<std::list<T> > &input)
    {
        // Use a std::set as our priority queue. This has the same complexity analysis as
        // a heap, but has a higher constant factor.
        // Implementing a min-heap is left as an exercise for the reader,
        // as is a non-mutating version
        std::set<std::list<T> *, cmp_list<T> > pq;
    
        for ( typename std::list<std::list<T> >::iterator it = input.begin();
                it != input.end(); it++)
        {
            if (it->empty())
                continue;
            pq.insert(&*it);
        }
    
        while (!pq.empty()) {
            std::list<T> *p = *pq.begin();
            pq.erase(pq.begin());
    
            output.push_back(p->front());
            p->pop_front();
    
            if (!p->empty())
                pq.insert(p);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need the following functionality Given two sorted lists, merge them I have this
BACKGROUND: I have a list of posts that need to be sorted their 'votes'
I have a method returning a list of String that need to be sorted.
I have sorted list of strings that I move between php and java. to
I have a list of objects, that are pre-sorted based on some complex criteria
I have this, and it works: # E. Given two lists sorted in increasing
I have a list of an object which need to be sorted depending on
I have a stl::list containing Widget class objects. They need to be sorted according
def sortProfiles(p): return sorted(p, key=itemgetter('first_name')) I have a list with dictionaries. This function allows
I have a set of objects, and I need to produce a sorted list,

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.