electrofriends.com  

...bringing innovative minds together       | HOME | ABOUT US | ARTICLES | SOURCE CODES | PROJECTS | EBOOKS |  FEEDBACK |  

PROGRAM TO SORT THE NUMBERS USING INSERTION SORT

Here is the program to sort the given integer in ascending order using insertion sort method. Please find the link to the pictorial tutor of the sorting. This program needs to enter the length of the entering array, followed by the array to be sorted. The entered integers are stored in the array A.

Logic : Here, sorting takes place by inserting a particular element at the appropriate position, that's why the name-  insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.

If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like selection sorting and bubble sorting, which follows in the next pages.

 

 


#include<stdio.h>
void main()
{
      int A[20], N, Temp, i, j;
      clrscr();
      printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
      scanf("%d", &N);
      printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
      for(i=1; i<=N; i++)
      {
             gotoxy(25,11+i);
             scanf("\n\t\t%d", &A[i]);
      }
      for(i=2; i<=N; i++)
      {
            Temp = A[i];
             j = i-1;
            while(Temp<A[j] && j>=1)
            {
                  A[j+1] = A[j];
                   j = j-1;
            }
            A[j+1] = Temp;
      }
      printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
      for(i=1; i<=N; i++)
      printf("\n\t\t\t%d", A[i]);
      getch();
}

Download exe and source code here.

Related source codes :
 

       Program to sort the numbers using Bubble sort

         Program to sort the numbers using Selection sort

         Select program from the list

 

 

 | HOME | ABOUT US | ARTICLES |  SOURCE CODES | PROJECTS |  SITEMAP |  EBOOKS | FEEDBACK |   



  Copyrights © 2005-2007 electrofriends.com, All rights reserved. webmaster@electrofriends.com