Insertion Sort Program in C

Implementing Insertion Sort Algorithm


#include<stdio.h>

int main()

{

  int arr[20],num,Temp,i,j;

  printf("\n\n How Many Number do you want to enter...: ");

  scanf("%d",&num);

  printf("\n Enter the elements of the array...:\n");

  for(i=0; i < num;i++)

  {

    scanf("%d", &arr[i]);

  }

  for(i=1; i< num; 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\t\t-------Elements after insertion Sort------\n\n");

  for(i=0; i < num; i++)

  {

    printf("\t%d", arr[i]);

  }


}

Leave a Reply

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