Search C Program

Insertion Sort

/* Insertion Sort */
#include<stdio.h>

int main()
{
 int arr[10],i,j,new;
 printf("Please enter 10 values:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);
 
 for(i=1;i<10;i++)
 {
  new=a[i];
  for(j=i-1;j>=0&&new<a[j];j--)
  {
   a[j+1]=a[j];
  }
  a[j+1]=new;
 }
  
 printf("Sorted Array is:\n");
 for(i=0;i<10;i++)
  printf("%d\n",arr[i]);
 
 return 0;
}

Related Posts:
-------------------------------------------------------------
Bubble Sort
http://cbasicprogram.blogspot.in/2012/05/bubble-sort.html

Selection Sort
http://cbasicprogram.blogspot.in/2012/05/selection-sort.html

1 comment:

  1. #include
    #include
    void main()
    {
    int a[10],i,flag=0,r,n;
    clrscr();
    printf("to replace an element in array\n");
    for(i=0;i<4;i++)
    {
    scanf("%d",&a[i]);
    }

    for(i=0;i<4;i++)
    {
    printf("%d\n",a[i]);
    }
    printf("enter a number to find if it exists or not\n");
    scanf("%d",&n);
    scanf("%d",&r);
    for(i=0;i<4;i++)
    {
    if(n==a[i])
    a[i]=r;
    }
    for(i=0;i<4;i++)
    {
    printf("%d\n",a[i]);
    }

    getch();
    }




    ReplyDelete