ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

C program source code for insertion sort or sorting using arrays

Updated on January 18, 2013


Following is the source code for the insertion type sorting in Arrays . Copy the whole source code and save it in notepad with the extension either *.c or *.cpp .

Then open the saved file with your compiler and run the program .


#include<stdio.h>
#include<conio.h>
int main()
{
    int a[100],n,k,i,j,temp;
    printf("Enter the no. of elements you want to enter :\n");
    scanf("%d",&n);
    printf("Enter the %d elements:\n",n);
        for(i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
        }
        for(k=1;k<=n-1;k++)
        {
            temp=a[k];
            j=k-1;
    while(temp<a[j]&&j>=0)
        {
            a[j+1]=a[j];
            j=j-1;
        }
            a[j+1]=temp;
        }
    printf("Elements of array after sorting are :");
        for(i=0;i<=n-1;i++)
        {
            printf("%2d",a[i]);
        }
    getch();
}

You can change the values in the source code according to your needs . The screenshot shows the output of the program and so is the proof of a working program .Enjoy programming :) .


Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article