Wednesday, October 20, 2021

Program 1- Insertion Sort in C language

Program 1: Insertion Sort
***************************************************

#include <stdio.h>
int main()
{   int i, j, n, temp, arr[25];
   printf("How many numbers u are going to enter?: ");
   scanf("%d",&n);
   printf("Enter %d elements: ", n);
   // This loop would store the input numbers in array
   for(i=0;i<n;i++)
      scanf("%d",&arr[i]);
   // Implementation of insertion sort algorithm
   for(i=1;i<n;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("Order of Sorted elements: ");
   for(i=0;i<n;i++)
      printf(" %d",arr[i]);
    return 0;}

Output