Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Monday, May 26, 2014

insertion sort in c

Insertion sort algorithm implementation in c

#include <stdio.h>
#include<conio.h> 
void main()
{
  int n, array[1000], i, j, t;
  clrscr();
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (i = 0; i < n; i++) 
  {
    scanf("%d", &array[i]);
  }
   for (i = 1 ; i <= n - 1; i++) 
   {
    j = i;
    while ( j > 0 && array[j] < array[j-1]) 
     {
       t = array[j];
      array[j] = array[j-1];
      array[j-1] = t;
      j--;
    }
  }
  printf("Sorted list in ascending order:\n");
  for (i = 0; i <= n - 1; i++) 
  {
    printf("%d\n", array[i]);
  }
  getch();
}

Output of program:


No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC