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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:49:25+00:00 2026-06-13T06:49:25+00:00

I am writing a sorting program that needs to take a an array, fill

  • 0

I am writing a sorting program that needs to take a an array, fill it with 1000 random numbers, then copy the array into a second array. After that the program is supposed to use the selectionSort function and the insertSort function .

But when I use the functions I am supposed to keep track of all the swaps and key comparisons. I have figured out how to do that for the insertSort. I cannot figure out how to do this for the selectionSort

Here is my code:

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

  for (loc = 0; loc < length; loc++)
  {
    minIndex = minLocation(list, loc, length - 1);

    swap(list, loc, minIndex);

   }
cout<<"swaps = "<<swaps<<endl;
  } //end selectionSort

 template <class elemType>
 void swap(elemType list[], int first, int second)
 {
 elemType temp;
  temp = list[first];
  list[first] = list[second];
  list[second] = temp;
  } //end swap

 template <class elemType>
 int minLocation(elemType list[], int first, int last)
 {
  int loc, minIndex;    
  minIndex = first;

  for (loc = first + 1; loc <= last; loc++)
   { if (list[loc] < list[minIndex])
        minIndex = loc;

}

   return minIndex;
  } //end minLocation

  template <class elemType>
  void insertionSort(elemType list[], int length)
   {
int swaps = 0;
int comp = 0;

   for (int firstOutOfOrder = 1; firstOutOfOrder < length;
                             firstOutOfOrder++)
    if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
    {
        elemType temp = list[firstOutOfOrder];
        int location = firstOutOfOrder;

        do
        {
            list[location] = list[location - 1];
            location--;
            comp +=1;   
        } while(location > 0 && list[location - 1] > temp);

        list[location] = temp;
         swaps +=1;
    }

cout<<"swaps = "<<swaps<<endl;
cout<<"comps = "<<comp<<endl;
   } //end insertionSort

 #include<iostream>
 #include <ctime>
 #include <cstdlib>
 #include "searchSortAlgorithms.h"

 using namespace std;

 int main (){
//generate a new random set each time 

srand(time(0));

int a[1000] = {0};
int b[1000] = {0};

for(int i= 0; i<1000; i++)
{
    a[i] = rand()% 1000;
    b[i] = a[i];
}


insertionSort(a, 1000);
selectionSort(b, 1000);

    return 0;
 }

The swaps and comparisons print out for the inertionSort, but I am unfamiliar with how I would get it to work with the selectionSort since the sort algorithm calls other functions in a for loop.
Any input would be appreciated it.

  • 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-13T06:49:26+00:00Added an answer on June 13, 2026 at 6:49 am

    Actually, your selection sort has a fixed number of comparations (length * (length-1) / 2) and swaps (length).

    If you want to count them anyway,

     // Count variables can be defined in main(), and passed through
     // to swap(), minLocation().
     template <class elemType>
     void swap(elemType list[], int first, int second)
     {
        // Count swap here  
     }
    
     template <class elemType>
     int minLocation(elemType list[], int first, int last)
     {
       ..
       for (loc = first + 1; loc <= last; loc++)
       { 
          // Count comparation here
       }   
       ..
     }
    

    By the way, your counting of comparations in insertion sort is not complete either.

    for (int firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++)
    {    
       // You miss the count here.
       if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a WPF program in C# that needs to render a set of
I'm trying to create a word sorting program that will read the words in
I am writing string sorting and I couldn't decide whether I should use array
I need some help with a program that I am writing for my Systems
I am writing a short program to sort an array of integers. I am
I'm a student just learning xml and c#. I'm writing a program that will
hey i am new to c# plz help. i am writing a program that
I know that Rails has sorting methods built into ActiveRecord, but I am just
I am writing a logger for a sorting function like this: bubble :: (Ord
I'm writing a piece of code for a school asignment, we're implementing sorting algorithms

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.