Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implementation of Insertion Sort in C

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 263
    Comment on it

    Insertion Sort:- The Insertion sort is used to sort an array. The array can be sorted in each round means, In each Iteration of an array the element can be find in sorted.

    Steps:-

    1: The second element of array compared with the elements will appears before it . If the second element is smaller than first element, they both will interchange their positions. After first iteration, first two elements of an array will be sorted.

    2. The third element of an array is compared with the elements that appears before it. If third element is smaller than first element, it is inserted in the position of first element. but in case If third element is larger than first element and smaller than second element, it is inserted in the position of second element. If third element is larger than both the elements, it is kept in the position as it is. After the completion of second iteration, first three elements of an array will be sorted.

    #include<stdio.h>
    #include<conio.h>
    
    
    void main()
    {
    int arr[10],i,j=0,temp=0;
    clrscr();
    printf("\nEnter 5 Elements in Array:=\n");
    for(i=0;i<5;i++)
    scanf("%d",&arr[i]);
    for(i=1;i<5;i++)
    {
    
         temp=arr[i];
         j=i-1;
         while(temp<arr[j]&&j>=0)
         {
        arr[j+1]=arr[j];
        j=j-1;
         }
    
        arr[j+1]=temp;
    }
    printf("\n\nSorted Array:=\n");
    for(i=0;i<5;i++)
    printf("\n%d",arr[i]);
    getch();
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: