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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:44:32+00:00 2026-06-14T08:44:32+00:00

I have to keep track of all comparisons in this selection sort, but when

  • 0

I have to keep track of all comparisons in this selection sort, but when I do only 1 is returned for the value out of a list of 1000. I am not sure I implemented it correctly but I am sure I placed the comparison count correctly/. Typically a selection sort has a fixed amount of key comparisons but our instructor has forbidden the use of formulas to track them. I am curious why this output keeps returning:

comp = 1
swap = 1 

template <class elemType>
void selectionSort(elemType list[], int length)
{
    int loc, minIndex;
    int first; 
    int last, second;
    int swaps =0;
    int comp = 0;

    minIndex = first;
    for (loc = 0; loc < length; loc++)
    {
        comp+=1; 

        for(loc = first +1; loc<=last; loc++)
        {
            comp+=1;
            if(list[loc]<list[minIndex])
                minIndex=loc;
            comp+=1;
        }
        elemType temp;
        temp= list[first];
        list[first]= list[second];
        list[second] = temp;


        swaps+=1;
    }

    //  comp = (length *(length-1)/2);

    cout<<"swaps= "<<swaps<<endl;
    cout<<"comps= "<<comp<< endl;
}

Any thoughts are appreciated

  • 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-14T08:44:33+00:00Added an answer on June 14, 2026 at 8:44 am

    I guess your sorting itself is not working. You need to properly initialize last, first and second variables (specially the last) and then move(increment) them.

    Since your last variable is defaulted with 0, its not performing the iterations because of loc<=last condition in the second for loop as you have desired. I think you need to initialize it as :

     last = length-1;
    

    There is one more issue: In both the loops, you are using the same index variable i.e. loc. I think you need to use two different variables.

    for (loc = 0; loc < length; loc++)
    {
      comp+=1; 
       for(loc = first +1; loc<=last; loc++)
       {
    

    Once you fix the logic so that it performs the full sorting, you will get the right count.

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

Sidebar

Related Questions

Say you have a lot of (key, value) objects to keep track of, with
I have a situation where I need to keep track of all changes to
I want to keep track of all the methods which have been run on
All, I have a site named xyz.com. To track the activities on this website
I have an imagemap and wanted to keep track of how many times it
I have a table that I keep track of fees for a specific item.
I have created a singleton class to keep track of my data on my
I have an ajax application that uses hashbanging for navigation and I keep track
I have various models of which I would like to keep track and collect
I have a MVVM application and I want to keep track of the focused

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.