Follow Us

LightBlog

Breaking

Wednesday, March 1, 2017

Write a c program for insertion sort ?

Write a c program for insertion sort ?

#include <stdio.h>
int main()
{
  int n, a[100], c, d, temp;

  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (c = 0; c < n; c++) 
{
    scanf("%d", &a[c]);
  }

  for (c = 1 ; c <= n - 1; c++) 
{
    d = c;
    while ( d > 0 && a[d] < a[d-1]) 
{
      temp = a[d];
      a[d]   = a[d-1];
      a[d-1] = temp;

      d--;
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c <= n - 1; c++) 
{
    printf("%d\n", a[c]);
  }

  return 0;
}

No comments:

Post a Comment