Implement the following sorting techniques to sort a given list of integers in ascending order (i) Insertion Sort

 #include<stdio.h>

void ins_sort(int [],int n);

int main()

{

    int n,a[20],i;

    clrscr();

    printf("enter n value:");

    scanf("%d",&n);

    printf("enter %d elements",n);

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

    {

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

    }

     ins_sort(a,n);

     return 0;

}

  void ins_sort(int a[],int n)

  {

    int i,j,ele,k;

    for(i=1;i<n;i++)

    {

     ele=a[i];

     j=i-1;

     while((ele<a[j]) && j>=0)

     {

       a[j+1]=a[j];

       j=j-1;

     }

       a[j+1]=ele;

printf("\n after pass %d : ",i);

       for(k=0;k<n;k++)

printf("%d\t",a[k]);

    }

  }

Comments

Popular posts from this blog

Create a Binary Search Tree of integers and perform the following operations (i)insert (ii) delete (iii). Search (iv) traversals (pre-order, in-order, post-order) BST

Creation Of DLL

Develop non-recursive functions to perform search for a Key value in a given list using (i) Binary Search Non Recursive