C Program For Insertion Sorting

#include<stdio.h>
#include<conio.h>
void insertion_sort(int a[20],int n);
 void main()
{
    auto int n,a[20];
    register int i,j,temp;
    clrscr();
    printf("enter the size of an array");
    scanf("%d",&n);
    printf("enter array elements");
    for(i=0;i<n;i++)
scanf("%d",&a[i]);
    insertion_sort(a,n);
    printf("the sorted elements are:\n");
    for(i=0;i<n;i++)
printf("\t%d",a[i]);
    getch();
 }
  void insertion_sort(int a[20],int n)
 {
  int temp,i,j;
  for(i=1;i<n;i++)
  {
  temp=a[i];
  for(j=i;j>0&&a[j-1]>temp;j--)

     a[j]=a[j-1];
     a[j]=temp;
    // a[i+1]=temp;

 }
 }
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment