Insertion sort in C program

Here is the program to sort the given integer in ascending order using insertion sort method. Please find the pictorial tutor of the insertion sorting.

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.

Insertion Sort Demo
Insertion Sort Demo

C program for Insertion Sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
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=0; i<N; i++)
	{
		scanf("\n\t\t%d", &A[i]);
	}
	for(i=1; i<N; i++)
	{
		Temp = A[i];
		j = i-1;
		while(Temp<A[j] && j>=0)
		{
			A[j+1] = A[j];
			j = j-1;
		}
		A[j+1] = Temp;
	}
	printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
	for(i=0; i<N; i++)
		printf("\n\t\t\t%d", A[i]);
	getch();
}

Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

194 thoughts on “Insertion sort in C program

  1. Great Explanation. I had read insertion sort so many times, but could not understand it, your example was superb. Thanks a lot!

  2. Thanks for your great explanation with an apt example.
    And here i am having a question what is the use of gotoxy
    and how is this working

  3. #include
    #include
    using namespace std;

    const int SIZE = 20;

    int binarySearch(string [], int, int);
    void selectionSort (string[], int);

    int main()
    {
    string nameSearch;
    string name[SIZE] =
    {“Collins, Bill”, “Smith, Bart”, “Michalski, Joe”, “Griffen, Jim”,
    “Sanchez, Manny”, “Rubin, Sarah”, “Taylor, Tyrone”, “Johnson, Jill”,
    “Allison, Jeff”, “Moreno, Juan”, “Wolfe, Bill”, “Whitman, Jean”,
    “Moretti, Bella”, “Wu, Hong”, “Patel, Renee”, “Harrison, Rose”,
    “Smith, Cathy”, “Conroy, Pat”, “Kelly, Sean”, “Holland, Beth”};

    cout <> nameSearch;

    return 0;
    }

    int binarySearch(string array[], int size, int value)
    {
    int first = 0,
    last = size – 1,
    middle,
    position = -1;
    bool found = false;

    while (!found && first value)
    last = middle – 1;
    else
    first = middle + 1;
    }
    return position;
    }
    void selectionSort(string array[], int size)
    {
    int startScan, minIndex, minValue;

    for(startScan = 0; startScan < (size – 1); startScan++)
    {
    minIndex = startScan;
    minValue = array[startScan];
    for (int index = startScan + 1; index < size; index++)
    {
    if (array[index] < minValue)
    {
    counter++;
    minValue = array[index];
    minIndex = index;
    }
    }
    array[minIndex] = array[startScan];
    array[startScan] = minValue;
    }
    return counter;
    }

  4. thank u, it’s simple nd more understanding, especially d apt work, dat
    is useful to understand more.

  5. #include
    #include
    void insertsort(int ar[],int n)
    {
    int i,j,t;
    for(i=1;it)
    {
    ar[j+1]=ar[j];
    j–;
    }
    ar[j+1]=t;
    }
    }
    void main()
    {
    int ar[10],i;
    clrscr();
    ar[0]=-32768;
    for(i=1;i<=9;i++)
    {
    scanf("%d",&ar[i]);
    }
    insertsort(ar,10);
    printf("\nSorted data\n");
    for(i=1;i<=9;i++)
    {
    printf("%d ",ar[i]);
    }
    getch();
    }

  6. void swap(int *src, int *dest)
    {
    int temp;
    temp = *src;
    *src = *dest;
    *dest = temp;
    }

    main ()
    {
    int a[] = {4,8,7,3,2,9,0,1,6,5};
    unsigned int n = sizeof(a)/sizeof(a[0]);
    int i=0, j=0;

    for(i=1; i=0; j–) {
    if(a[j] > a[j+1]) {
    swap(&a[j+1], &a[j]);
    }
    }
    }

    for(i=0; i<n; i++)
    printf("Sorted list : %d ", a[i]);

    return 0;
    }

  7. Its very helpful for me,now every confusion has been finished…………..so thank you so so …………..much.

  8. Its very helpful for me,now every confusion has been cleared…………..so thank you so so …………..much.

  9. #include
    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=0; i<N; i++)
    {
    gotoxy(25,11+i);
    scanf("\n\t\t%d", &A[i]);
    }
    for(i=1; i<N; i++)
    {
    Temp = A[i];
    j = i-1;
    while(Temp=0)
    {
    A[j+1] = A[j];
    j = j-1;
    }
    A[j+1] = Temp;
    }
    printf(“\n\tTHE ASCENDING ORDER LIST IS…:\n”);
    for(i=0; i<N; i++)
    printf("\n\t\t\t%d", A[i]);
    getch();
    }

  10. i want print like this
    .1
    A B
    2 3 4
    C D E F . . . .like this any one help me write the coding for that

  11. hii..i want to know the meaning of why we use j=j-1 after assigning temp to a[i] in insertion sort..please explain it as soon as possible my mail id is gchetna30@gmail.com.

  12. main()
    #define size 10
    {
    int arr[size],i,j,item;
    printf(“\n enter the element in array”);
    for(i=0;i<size;i++)
    scanf("%d",&arr[i]);
    for(j=0;j<size;j++)
    { item=arr[i];
    for(i=j-1;item=0;i–)
    { arr[i+1]=arr[i];
    arr[i]=item;
    }}

  13. #include
    #include
    void main()
    {
    int a[20],k,n,i=0,j=0,t=0;

    clrscr();
    printf(“no of of elemts”);
    scanf(“%d”,&n);
    printf(“enter %d values”,n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    for(i=1;i=1;j–)
    {

    printf(“i=%d j=%d %d %d”,i,j,a[j],a[j-1]);
    if(a[j]>a[j-1])
    break;
    else
    {
    t=a[j];
    a[j]=a[j-1];
    a[j-1]=t;
    }

    } printf(“\n”);
    } printf(“\n”);

    for(i=0;i<n;i++)
    printf(" %d ",a[i]);
    getch();
    }

    THE ABOVE CODE REPRESENTS WHICH SORTING TECHNIQUE????

    DOES IT REPRESENT INSERTION SORT????

  14. m confused in insertion sort …..when comparison is done and 1st pass completes then how elements compare with all the previous elements there is no loop ……pls explain…..

  15. Well explained. I assume there is one error in the code during runtime. Changed below code.

    while( j>=0 && Temp<A[j] )
    {
    A[j+1] = A[j];
    j = j-1;
    }

  16. #include
    using namespace std;
    int main()
    { int k,A[6];
    for(int i=0;i>A[i];
    }
    for(int i=1;i=0 && A[j]>A[i])
    {
    A[j+1]=A[j];
    j–;
    }
    A[j+1]=value;
    }
    cout<<"sorted list"<<endl;
    for( k=0;k<6;k++)
    {
    cout<<A[k]<<"\t";
    }
    return 0;
    }
    /* wht give the wrong output?? i tryed to check it …plz help*/

  17. Source code of simple merge sort implementation using array in ascending order in c programming language

    #include
    #define MAX 50

    void mergeSort(int arr[],int low,int mid,int high);
    void partition(int arr[],int low,int high);

    int main(){

    int merge[MAX],i,n;

    printf(“Enter the total number of elements: “);
    scanf(“%d”,&n);

    printf(“Enter the elements which to be sort: “);
    for(i=0;i<n;i++){
    scanf("%d",&merge[i]);
    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
    printf("%d ",merge[i]);
    }

    return 0;
    }

    void partition(int arr[],int low,int high){

    int mid;

    if(low<high){
    mid=(low+high)/2;
    partition(arr,low,mid);
    partition(arr,mid+1,high);
    mergeSort(arr,low,mid,high);
    }
    }

    void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

    if(arr[l]mid){
    for(k=m;k<=high;k++){
    temp[i]=arr[k];
    i++;
    }
    }
    else{
    for(k=l;k<=mid;k++){
    temp[i]=arr[k];
    i++;
    }
    }

    for(k=low;k<=high;k++){
    arr[k]=temp[k];
    }
    }

    Sample output:

    Enter the total number of elements: 5
    Enter the elements which to be sort: 2 5 0 9 1
    After merge sorting elements are: 0 1 2 5 9

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in