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 7415231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:15:38+00:00 2026-05-29T07:15:38+00:00

I have a collection of about a hundred or so sorted vector<int> ‘s Although

  • 0

I have a collection of about a hundred or so sorted vector<int>‘s Although most vectors have a small number of integers in them, some of the vectors contain a large (>10K) of them (thus the vectors don’t necessarily have the same size).

What I’d like to do essentially iterate through smallest to largest integer, that are contained in all these sorted vectors.

One way to do it would be to merge all these sorted vectors into a sorted vector & simply iterate. Thus,

Question 1: What is the fastest way to merge sorted vectors into a sorted vector?

I’m sure on the other hand there are faster / clever ways to accomplish this without merging & re-sorting the whole thing — perhaps popping the smallest integer iteratively from this collection of sorted vectors; without merging them first.. so:

Question 2: What is the fasted / best way to pop the least element from a bunch of sorted vector<int>‘s?


Based on replies below, and the comments to the question I’ve implemented an approach where I make a priority queue of iterators for the sorted vectors. I’m not sure if this is performance-efficient, but it seems to be very memory-efficient. I consider the question still open, since I’m not sure we’ve established the fastest way yet.

// compare vector pointers by integers pointed
struct cmp_seeds {
    bool operator () (const pair< vector<int>::iterator, vector<int>::iterator> p1, const pair< vector<int>::iterator, vector<int>::iterator> p2) const {
        return *(p1.first) >  *(p2.first);      
    }
};

int pq_heapsort_trial() {

    /* Set up the Sorted Vectors */ 
    int a1[] = { 2, 10, 100};
    int a2[] = { 5, 15, 90, 200};
    int a3[] = { 12 };

    vector<int> v1 (a1, a1 + sizeof(a1) / sizeof(int));
    vector<int> v2 (a2, a2 + sizeof(a2) / sizeof(int));
    vector<int> v3 (a3, a3 + sizeof(a3) / sizeof(int));

    vector< vector <int> * > sorted_vectors;
    sorted_vectors.push_back(&v1);
    sorted_vectors.push_back(&v2);
    sorted_vectors.push_back(&v3);
    /* the above simulates the "for" i have in my own code that gives me sorted vectors */

    pair< vector<int>::iterator, vector<int>::iterator> c_lead;
    cmp_seeds mycompare;

    priority_queue< pair< vector<int>::iterator, vector<int>::iterator>, vector<pair< vector<int>::iterator, vector<int>::iterator> >, cmp_seeds> cluster_feeder(mycompare);


    for (vector<vector <int> *>::iterator k = sorted_vectors.begin(); k != sorted_vectors.end(); ++k) {
        cluster_feeder.push( make_pair( (*k)->begin(), (*k)->end() ));
    }


    while ( cluster_feeder.empty() != true) {
        c_lead = cluster_feeder.top();
        cluster_feeder.pop();
        // sorted output
        cout << *(c_lead.first) << endl;

        c_lead.first++;
        if (c_lead.first != c_lead.second) {
            cluster_feeder.push(c_lead);
        }
    }

    return 0;
}
  • 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-29T07:15:38+00:00Added an answer on May 29, 2026 at 7:15 am

    One option is to use a std :: priority queue to maintain a heap of iterators, where the iterators bubble up the heap depending on the values they point at.

    You could also consider using repeating applications of std :: inplace_merge. This would involve appending all the data together into a big vector and remembering the offsets at which each distinct sorted block begins and ends, and then passing those into inplace_merge. This would probably be faster then the heap solution, although I think fundamentally the complexity is equivalent.

    Update: I’ve implemented the second algorithm I just described. Repeatedly doing a mergesort in place. This code is on ideone.

    This works by first concatenating all the sorted lists together into one long list. If there were three source lists, this means there are four ‘offsets’, which are four points in the full list between which the elements are sorted. The algorithm will then pull off three of these at a time, merging the two corresponding adjacent sorted lists into one sorted list, and then remembering two of those three offsets to be used in the new_offsets.

    This repeats in a loop, with pairs of adjacent sorted ranges merged together, until only one sorted range remains.

    Ultimately, I think the best algorithm would involve merging the shortest pairs of adjacent ranges together first.

    // http://stackoverflow.com/questions/9013485/c-how-to-merge-sorted-vectors-into-a-sorted-vector-pop-the-least-element-fro/9048857#9048857
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cassert>
    using namespace std;
    
    template<typename T, size_t N>
    vector<T> array_to_vector( T(*array)[N] ) { // Yes, this works. By passing in the *address* of
                                                // the array, all the type information, including the
                                                // length of the array, is known at compiler. 
            vector<T> v( *array, &((*array)[N]));
            return v;
    }   
    
    void merge_sort_many_vectors() {
    
        /* Set up the Sorted Vectors */ 
        int a1[] = { 2, 10, 100};
        int a2[] = { 5, 15, 90, 200};
        int a3[] = { 12 };
    
        vector<int> v1  = array_to_vector(&a1);
        vector<int> v2  = array_to_vector(&a2);
        vector<int> v3  = array_to_vector(&a3);
    
    
        vector<int> full_vector;
        vector<size_t> offsets;
        offsets.push_back(0);
    
        full_vector.insert(full_vector.end(), v1.begin(), v1.end());
        offsets.push_back(full_vector.size());
        full_vector.insert(full_vector.end(), v2.begin(), v2.end());
        offsets.push_back(full_vector.size());
        full_vector.insert(full_vector.end(), v3.begin(), v3.end());
        offsets.push_back(full_vector.size());
    
        assert(full_vector.size() == v1.size() + v2.size() + v3.size());
    
        cout << "before:\t";
        for(vector<int>::const_iterator v = full_vector.begin(); v != full_vector.end(); ++v) {
                cout << ", " << *v;
        }       
        cout << endl;
        while(offsets.size()>2) {
                assert(offsets.back() == full_vector.size());
                assert(offsets.front() == 0);
                vector<size_t> new_offsets;
                size_t x = 0;
                while(x+2 < offsets.size()) {
                        // mergesort (offsets[x],offsets[x+1]) and (offsets[x+1],offsets[x+2])
                        inplace_merge(&full_vector.at(offsets.at(x))
                                     ,&full_vector.at(offsets.at(x+1))
                                     ,&(full_vector[offsets.at(x+2)]) // this *might* be at the end
                                     );
                        // now they are sorted, we just put offsets[x] and offsets[x+2] into the new offsets.
                        // offsets[x+1] is not relevant any more
                        new_offsets.push_back(offsets.at(x));
                        new_offsets.push_back(offsets.at(x+2));
                        x += 2;
                }
                // if the number of offsets was odd, there might be a dangling offset
                // which we must remember to include in the new_offsets
                if(x+2==offsets.size()) {
                        new_offsets.push_back(offsets.at(x+1));
                }
                // assert(new_offsets.front() == 0);
                assert(new_offsets.back() == full_vector.size());
                offsets.swap(new_offsets);
    
        }
        cout << "after: \t";
        for(vector<int>::const_iterator v = full_vector.begin(); v != full_vector.end(); ++v) {
                cout << ", " << *v;
        }
        cout << endl;
    }
    
    int main() {
            merge_sort_many_vectors();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of about 10,000 small VBScript programs (50-100 lines each) and
I have this problem: I have a collection of small files that are about
I have a collection of about 170,000 words and I perform a number of
I have a collection with about 500000 dataset in it and I like to
I have a collection of about 8,000 test scores in an XML file. Using
I have a collection of synopses that are about 1000 char long each. In
I have a Collection as Vector<HashMap<String, String>> Actually I am using this as list
Suppose you have a collection of a few hundred in-memory objects and you need
I have a collection of about 3500 product Serial Numbers that my software uses
Once again a question about the garbage collector in actionscript-3: If I have a

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.