Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Program to sort elements in Ascending Order using Insertion Sort in C language

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 641
    Comment on it

    Program to sort elements in Ascending Order using Insertion Sort in C language

    Insertion Sort
    Definition:- It is a sorting which sort one number at a time & insert to its proper location in the array list until all the numbers are sorted from the list.

    //Header Files
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
      int n, i, k, ptr, temp, arr[20];
      clrscr();            //to clear screen
      // User to input the limit of the array
      printf("\n Enter the Element limit:-");
      scanf("%d",&n);                // To scan the limit of the array
      //Asking user to input the elements of the array
      printf("\n Enter the element to be sorted:->\n");
      for(i=0;i<n;i++){
         scanf("%d", &arr[i]);  //Scanning the elements in the array
      }
      // inserting the element in the correct place in the array
      for(k=1; k<n;k++){
         temp = arr[k];
         ptr = k-1;
         while((temp<arr[ptr]) && (ptr>=0)) {
          arr[ptr+1] = arr[ptr];
              ptr = ptr-1;
         }
          arr[ptr+1] = temp;
       }
    
       printf("\n Insertion sorted elements are:-");
       for(i=0;i<n;i++)
         {
          printf("\n%d",arr[i]);  //Printing the Sorted array
         }
      getch();
    }
    

    Output:-

    Enter the Element limit:-6
    
    Enter the element to be sorted:->
    9
    5
    7
    0
    2
    4
    
    Insertion sorted elements are:-
    0
    2
    4
    5
    7
    9
    
    Program to sort elements in Ascending Order using Insertion Sort in C language

 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: