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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:03:28+00:00 2026-06-02T08:03:28+00:00

I have a std::list which I am currently randomizing using a Fisher-Yates shuffle (see

  • 0

I have a std::list which I am currently randomizing using a Fisher-Yates shuffle (see http://en.wikipedia.org/wiki/Fisher-Yates_shuffle). To summarize, my code carries out the following steps on the list:

  1. Loop through each element of the list.
  2. Swap the element with a randomly chosen element from the current position onwards, including itself.

Because lists don’t provide random access, this means that I am iterating over the entire list in step 1, and for each element I’m iterating again, on average over half the remaining elements from that point onwards. This is a major bottleneck in my program’s performance, so I’m looking to improve it. For other reasons I need to continue using list as my container, but I’m considering converting to a vector at the start of my randomize function, and then converting back to list at the end. My lists typically contain 300 – 400 items, so I would guess that the cost of conversion between containers will be worth it to avoid traversing the items sequentially.

My question is: does this seem like the best way to go about optimizing the code? Is there a better way?

  • 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-06-02T08:03:29+00:00Added an answer on June 2, 2026 at 8:03 am

    One easy improvement is to copy the data into a vector, shuffle the vector, and copy it back into a list. That is what was suggested in comments by Max and PeskyGnat:

    vector<int> myVector(myList.size());
    copy(myList.begin(), myList.end(), myVector.begin());
    random_shuffle(myVector.begin(), myVector.end());
    list<int> myListShuffled(myVector.begin(), myVector.end());
    

    This implementation is pretty fast. But, it will do three passes over the vector, and you can get it down to two passes by implementing the shuffle yourself:

    vector<int> myVector(myList.size());
    int lastPos = 0;
    for(list<int>::iterator it = myList.begin(); it != myList.end(); it++, lastPos++) {
        int insertPos = rand() % (lastPos + 1);
        if (insertPos < lastPos) {
            myVector[lastPos] = myVector[insertPos]; 
        }
    
        myVector[insertPos] = *it;
    }
    
    list<int> myListShuffled(myVector.begin(), myVector.end());
    

    Since the first version is much easier to understand and much less error-prone, it’s almost always preferable… unless perhaps this bit of code is critical for your performance (and you confirmed that with measurement.)

    EDIT: By the way, since you are looking at the Wikipedia article, the second code sample uses the “inside-out” variant of Fisher-Yates.

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

Sidebar

Related Questions

I have some code which deals with various std::list objects, and I am currently
I have one std::list<> container and these threads: One writer thread which adds elements
I have an std::list<TYPE> which may contain child classes of TYPE. I need to
I have a class Class which has a member std::list, I want to search
I have a animation class which stores the frames in a std::list, the header
I have some std::list<char> list_type Now I have to supply contents of the list
I have a std::list of boost::shared_ptr<T> and I want to remove an item from
Say you have a std::list of some class. There are two ways you can
I have a snippet of code like this: std::list<boost::shared_ptr<Point> > left, right; // ...
I have a class that includes a std::list and wish to provide public begin()

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.