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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:35:26+00:00 2026-06-05T21:35:26+00:00

Im using c++ and is using insertion sort Where in the insertion sort algoithm

  • 0

Im using c++ and is using insertion sort

Where in the insertion sort algoithm should we put a counter to monitor number of item movements and number of item comparison. I have included my setup below

void InsertionSort::insertion_sort()
{
    int key,i,count = 0;
for(int j=1;j<10;j++)
 {
    key=Arr1[j];
    i=j-1;
    while(Arr1[i]>key && i>=0)
    {
     Arr1[i+1]=Arr1[i];
     i--;
     numberOfItemMovements++;
    }
    Arr1[i+1]=key;
}
}
}

as you can see, i cant seem to figure out where comparison counter should be put, although the item movement counter is good and work as expected. thanks

  • 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-05T21:35:28+00:00Added an answer on June 5, 2026 at 9:35 pm

    A way of getting this to work is using numberOfComparisons in the loop.

    while(++numberOfComparisons && Arr1[i]>key && i>=0)
    {
        Arr1[i+1]=Arr1[i];
        numberOfItemMovements++;
        i--;
    }
    

    1) Do you understand why ++numberOfComparisons can be used in the while loop but numberOfComparisons++ would fail?

    2) You have a problem where Arr1[-1] could be evaluated. Can you figure out where this is happening and how you can change your code to fix it?

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

Sidebar

Related Questions

I have to sort an array of 10 numbers using insertion sort and display
Hi I am using the following insertion sort algorithm and I would like to
I am trying to sort an array using the insertion sort algorithm. The array
I created this insertion sort for a data structures and algorithms about a year
I'm working on a program using doubly linked lists. I have already gotten the
Java 6's mergesort implementation in Arrays.java uses an insertion-sort if the array length is
I am trying a simple insertion sort. But the swapping I am trying isn't
Ok... I have this struct and comparison function- struct Edge { char point1; char
I have an input file that I want to sort based on timestamp which
class Sort { int[] arr; int counter=0; //Constructor public Sort() { arr = new

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.