tags:

views:

72

answers:

3

Goal is, I've multiple lists of elements available, and I want to be able to store all of these elements into a resultant list in an ordered way.

Some of the ideas that comes to my mind are a) Keep the result as a set (std::set), but the B-tree , needs to rebalanced every now and then. b) Store all the elements in a list and sort the list at the end.

But, I thought, why not store them in a sorted fashion, as and when we add the items to the resultant list.

Here is my function, that does the job of maintaining the results in a sorted way. Is there an efficient way to do the same?

void findItemToInsertAt(std::list<int>& dataSet, int itemToInsert, std::list<int>::iterator& location)
{
    std::list<int>::iterator fromBegin = dataSet.begin();
    std::list<int>::iterator fromEnd = dataSet.end() ;
    // Have two pointers namely end and begin
    if ( !dataSet.empty() )
        --fromEnd;

    // Set the location to the beginning, so that if the dataset is empty, it can return the appropriate value
    location = fromBegin;
    while ( fromBegin != dataSet.end()  )
    {
        // If the left pointer points to lesser value, move to the next element
        if ( *fromBegin < itemToInsert )
        {
            ++fromBegin;
            // If the end is greater than the item to be inserted then move to the previous element
            if ( *fromEnd > itemToInsert )
            {
                --fromEnd;
            }
            else
            {
                // We move only if the element to be inserted is greater than the end, so that end points to the
                // right location
                if ( *fromEnd < itemToInsert )
                {
                    location = ++fromEnd;
                }
                else
                {
                    location = fromEnd;
                }
                break;
            }
        }
        else
        {
            location = fromBegin;
            break;
        }
    }

}

And, here is the caller of the function

void storeListToResults(const std::list<int>& dataset, std::list<int>& resultset)
{

    std::list<int>::const_iterator curloc;
    std::list<int>::iterator insertAt;

    // For each item in the data set, find the location to be inserted into
    // and insert the item.
    for (curloc = dataset.begin(); curloc != dataset.end() ; ++curloc)
    {
        // Find the iterator to be inserted at
        findItemToInsertAt(resultset,*curloc,insertAt);
        // If we have reached the end, then the element to be inserted is at the end
        if ( insertAt == resultset.end() )
        {
            resultset.push_back(*curloc);
        }
        else if ( *insertAt != *curloc ) // If the elements do not exist already, then insert it.
        {
            resultset.insert(insertAt,*curloc);
        }
    }
}
A: 

I would sort the indivual lists and then use STL's list::merge to create the result list. Then, if the list is kind of big, you could pay to transfer the result to a vector.

Peter G.
But to use list::merge, the items have to be sorted first. But with respect to my example, the items in the source list are not sorted.
@nsivakr Yes, I noticed about the time you commented and edited the answer accordingly.
Peter G.
+2  A: 

At a glance, your code looks like it's doing a linear search of the list in order to find the place to insert the item. While it's true that std::set will have to balance its tree (I think it's a Red-Black Tree) in order to maintain efficiency, chances are it'll do so much more efficiently than what you're proposing.

David
I'm writing two different solutions, one with std::set and the other one with list and inserting into the list appropriately.But wondering, is there a faster way than this to insert into the list.
@nsivakr Not that I can think of. Linked-lists can't be binary searched; arrays (or vectors) can, but inserting into them is inefficient for other reasons. Putting them in an **array/vector** (not a list) and sorting them afterwards is about as efficient as using a `std::set`, but if you're **really** concerned about memory usage overhead then you might want to opt for a sorting algorithm (specifically heapsort or quicksort) rather than a tree structure which will take up extra memory for each node.
David
Thanks David. Appreciate your detailed explanation.
+1  A: 

Answering the question asked:

Is there an efficient way to do the same?

Yes. Use std::set.

pkh