By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

What is wrong with my Insertion Sort function?

P: n/a
I really don't know to do this... it is only doing the substitution/swapping once, when it should be doing that multiple times so as to arrange everything properly in ascending order... Here's the code:

Expand|Select|Wrap|Line Numbers
  1. void insertion(int a[], int size)
  2. {size+=1;
  3.  for(i=size-1;i>0;i--)
  4.      a[i]=a[i-1];
  5.  a[0]=INT_MIN;
  6.  for(i=1;i<size;i++)
  7.     {
  8.      temp=a[i];
  9.      j=i-1;
  10.      while(temp<a[j])
  11.         {
  12.          a[i]=a[j];
  13.          j--;
  14.         }
  15.      a[j+1]=temp;
  16.      cout<<"\n\nArray after pass "<<i<<" is - ";
  17.      for(k=1;k<size;k++)
  18.         cout<<a[k]<<" ";
  19.     }
  20.  for(i=1;i<size;i++)
  21.     a[i-1]=a[i];
  22.  size-=1;
  23.  cout<<"\n\nThe fully sorted array is - ";
  24.  display(a, size);
  25.  return;
  26. }
Oct 7 '10 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 2,398
Please describe the algorithm for Insertion Sort. Use natural language, not a computer program.
Oct 7 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.