C/C++ Why is my insertion sort code not working for certain array sizes?

  • Thread starter Thread starter waver.
  • Start date Start date
  • Tags Tags
    C++ Sort
AI Thread Summary
The discussion revolves around a coding issue with an insertion sort implementation in C++. The code works correctly for certain array sizes but fails for others, specifically when the user inputs n=4, leading to incorrect sorting and errors. The primary problem identified is the mixing of 0-based and 1-based indexing, which causes off-by-one errors. Suggestions include changing the loop condition from <= to < to prevent indexing beyond the array's bounds. Additionally, using a debugger to step through the code and inspect variable values is recommended as a method to identify errors. Print statements can also be helpful for tracking variable states at key points in the code.
waver.
Messages
8
Reaction score
0
hi Guys
i wrote a insertion sort code which is
Code:
#include <iostream> /*Waver*/
using namespace std;
int main()

{
int n,i,m;
float a[100],k;

cout<<"enter the amount of number";
cin>>n;

for(i=0; i<n; i++)
cin>>a[i];

for(i=1; i<=n; i++)
{
    for(m=1 ; m<=i; m++)
    {
      if(a[m]<a[m-1])
      {
          k=a[m-1];
          a[m-1]=a[m];
          a[m]=k;
      }

    }
}
for(i=0; i<n; i++)
cout<<a[i] <<"  ";
return 0;
}
it work fine with some size used in array and won't work with other size
i mean when i enter n=5 it work fine when i enter n=4 it won't work and sort is wrong

when n=5

fy3fnp.jpg
and when n=4
the sort is wrong and error appear with last number
s5l6jl.jpg


i just want to know what i did wrong . thanks in advance
 
Technology news on Phys.org
Hint: You are mixing 0-based and 1-based indexing in your logic. This is not a good idea. Read about off-by-one errors, and go from there.
 
I would use your debugger and step through the code making sure you agree with what values the computer has set. Doing this you should be able to locate your mistake.

If you think that's too difficult then sprinkle print statements after key lines to see values of indexes and other key variables.
 
jedishrfu said:
I would use your debugger and step through the code making sure you agree with what values the computer has set. Doing this you should be able to locate your mistake.

If you think that's too difficult then sprinkle print statements after key lines to see values of indexes and other key variables.

the debugger didnt cought any thing
14vc6h.jpg
 
Change the second (or the third) for loop to use < instead of <=. You're indexing beyond the end of the array.
 
When I said use the debugger to step through your code, I didn't mean run your code in the debugger.

The debugger can literally step one line at a time and as you do that you can inspect the values of any variable to see if they are what you expect. Many times you will see your error right away.

Or in the case of @rcgldr's hint you will discover that you looped one too many times.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top