Close
Close

Insertion Sort in C

   AyushChandel

Can anyone please tell with full explaination code for insertion sort using arrays?

I am not able to understand it through the website


Answers

  •   

    Have a look at https://www.codesdope.com/blog/article/sorting-an-array-using-insertion-sort-in-c/

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int a[] = {16, 19, 11, 15, 10, 12, 14};
        int i,j;
    
        for(i=0;i<7;i++)
        {
            for(j=i; j>0; j--)
            {
                //not in order
                if(a[j-1] > a[j])
                {
                    //swapping
                    int temp = a[j-1];
                    a[j-1] = a[j];
                    a[j] = temp;
                }
                //in order
                else
                {
                    break;
                }
            }
        }
    
        for(i=0;i<7;i++)
            printf("%d\n",a[i]);
    
        return 0;
    }
    

     


    • im not able to get that please solve it using for loop??
      - AyushChandel
    • I have edited the answer and written the code using for loop only.
      - Amit Kumar

Ask Yours
Post Yours
Write your answer