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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:26:30+00:00 2026-06-13T00:26:30+00:00

If I have an array A = <0, 15, 5, 1, 0, 20, 25,

  • 0

If I have an array A = <0, 15, 5, 1, 0, 20, 25, 30, 35, 40>. When i write the code to count the comparisons, I am confused on where to add a counter, because I’m afraid there might be repeated counts.

Nevertheless, it says there are 15 comparisons. I am not sure this is right. How many comparisons are there really?

int InsertionSort(int A[], int n)
{
   int i, j, index, counter = 0;

   for (i=1; i < n; i++)
   {
      index = A[i];
      for (j=i-1;j >= 0 && A[j] > index;j--)
      {
         A[j + 1] = A[j];
         counter++;
      }
      A[j+1] = index;
      counter++;
   }
   return counter;
}

int main()
{
    int A[]= {5,4,3,2,1};
    int counter = 0;
    int n =5;
    counter = InsertionSort(A, n);
    printf("%d",counter);

    return 0;
}
  • 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-13T00:26:31+00:00Added an answer on June 13, 2026 at 12:26 am

    To me, your counter appears to be on the wrong spot. Let’s say A=<3, 2>, then your algorithm would use 1 comparison, but would report counter=2. If 15 is the right answer, then this error did not occur or got canceled out somehow.

    To find out if 15 really is the right answer, this is how you can improve the counter. First of all, your algorithm relies on a left-to-right evaluation order of conditions (which most programming language adhere to). What this means is that if P=false then Q is not evaluated in (P && Q). If left-to-right evaluation order is not guaranteed, then the algorithm could potentially evaluate A[-1] > index (something which would crash your program). The easiest way to count correctly is to split the conjunction of the for-loop into two separate lines as follows:

    for (i=1; i < n; i++)
    {
       index = A[i];
       for (j=i-1; j >= 0; j--)
       {
          // Every time this line is reached, a comparison will be performed
          counter++;
          if (A[j] > index)
          {
             A[j + 1] = A[j];
          }
       }
       A[j+1] = index;
    }
    

    If this works out, please let us know the result and please up-vote this answer.

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

Sidebar

Related Questions

I have the following code which uses an array to write the result to
I have javascript code: var newPositions = new Array(count); for (var i = 0;
I am using apache.commons.net.telnet . I have char[] array . I am calling TelnetClient.getOutputStream().write(array[i]).
i have an char array b[20] which i want to write into a file
I have a 13x13x100 array, L, of doubles that I write out using csvwrite(L,
I have an 2 dimensional array. I write 3 values into the array during
I have a task where i need to write a multidimensional array to HDFS.
So I have the following read and write file program for an Array. How
I am trying to write PHP code to loop through an array to create
I have an array of structs I would like to write to a binary

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.