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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:49:54+00:00 2026-05-29T10:49:54+00:00

I am attempting to implement a function that sorts a randomly generated vector using

  • 0

I am attempting to implement a function that sorts a randomly generated vector using selection sort. I am trying a naive way just to see if I can get it working correctly. Here is my attempt:

void selection_sort(std::vector<int>& v)
{
    int pos, min, i;
    //std::vector<int>::iterator pos, min, i;

    for( pos = v[0]; pos < v[30]; ++pos)
    {
        min = pos;

        for( i = v[pos + 1]; i < v[30]; ++i)
        {   
            if( i < min)
            {
                min = i;
            }
        }

        if( min != pos)
        {   
            std::swap(v.at(min), v.at(pos));

        }
    }
}

For some reason however when I display the vector again, all of the elements are in the exact same order as they were originally. I am not sure if I am not using std::swap correctly or if my selection sort is not written correctly. I am sure the answer is trivially easy, but I can not see it. Thanks for your help in advance.

  • 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-29T10:49:55+00:00Added an answer on May 29, 2026 at 10:49 am

    Your problem is that you’re trying to base your loops around the actual values in the vector, not the indexes in the vector.

    So if your vector is randomly generated, and you say this:

    for( pos = v[0]; pos < v[30]; ++pos)
    

    There is a chance that the value at v[0] is greater than v[30]. Thus the loop would never run. I see the same problem in this loop:

    for( i = v[pos + 1]; i < v[30]; ++i)
    

    So I’d recommend using indexes for the actual looping. Try something like:

    for( pos = 0; pos < 30; ++pos)
    {
      min = v[pos];
    

    etc…

    EDIT: As mentioned below, it would also be better to base your loop of the size of the of the vector. However, to save your self from calling the expensive size() method every time the loops runs, just grab the size before the loop starts. For example:

    size_t size = v.size();
    for(size_t pos = 0; pos < size; ++pos)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to override and implement my own TabExpansion. In the function I want
I'm attempting to implement nested object forms for my site, using Ryan Daigle's blog
I'm attempting to implement OpenID with ASP.NET MVC (Yeah, we haven't heard that one
So I'm attempting to implement a hash table that will hash structures containing words.
I'm trying to implement a comet style, long polling connection using an XMLHttpResponse object.
I'm attempting to implement quicksort in-place, using boost's threading facility. Unfortunately I'm getting an
I am trying to implement an eqivilent version of perl's chomp() function in C
I'm attempting to implement the Tcl dictionary sort in Perl in order to order
Attempting to implement a poor man's test of whether a process is still running
I am currently attempting to implement a custom gridview interface to display data from

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.