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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top