What is wrong with my Insertion Sort function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Laya Maheshwari

    What is wrong with my Insertion Sort function?

    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:

    Code:
    void insertion(int a[], int size)
    {size+=1;
     for(i=size-1;i>0;i--)
    	 a[i]=a[i-1];
     a[0]=INT_MIN;
     for(i=1;i<size;i++)
    	{
    	 temp=a[i];
    	 j=i-1;
    	 while(temp<a[j])
    		{
    		 a[i]=a[j];
    		 j--;
    		}
    	 a[j+1]=temp;
    	 cout<<"\n\nArray after pass "<<i<<" is - ";
    	 for(k=1;k<size;k++)
    		cout<<a[k]<<" ";
    	}
     for(i=1;i<size;i++)
    	a[i-1]=a[i];
     size-=1;
     cout<<"\n\nThe fully sorted array is - ";
     display(a, size);
     return;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please describe the algorithm for Insertion Sort. Use natural language, not a computer program.

    Comment

    Working...