C PROGRAM FOR INSERTION SORT





Source code of simple insertion sort implementation using array in ascending order in c programming language

#include<stdio.h>
int main(){

  int i,j,s,temp,a[20];

  printf("Enter total elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);

  for(i=1;i<s;i++){
      temp=a[i];
      j=i-1;
      while((temp<a[j])&&(j>=0)){
      a[j+1]=a[j];
          j=j-1;
      }
      a[j+1]=temp;
  }

  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);

  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting:  0 2 3 7 9





5. Write a c program for heap sort.
7. Write a c program for shell sort.

16 comments:

  1. in your insertion sort swapping of value is not possible
    because
    you assign value of a[j] to a[j+1]
    but not alloting 0 anywhere in array

    ReplyDelete
    Replies
    1. 0 is allotting in j=i-1 which then proceed.....

      Delete
  2. #include

    int main()
    {
    int a[3],i,j,temp;
    printf("Enter 5 Array Element:\n");
    for(i=0;i<5;i++)
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<5;i++)
    {
    temp=a[i+1];
    j=i;
    while(temp=0)
    {
    a[j+1]=a[j];
    j--;
    }
    a[j+1]=temp;
    }
    printf("Sorted Array id: ");
    for(i=0;i<3;i++)
    {
    printf("%d",a[i]);
    }
    return 0;
    }

    ReplyDelete
  3. tu muy bien

    ReplyDelete
  4. hey this is a great program for insertion sort !!!!!!

    ReplyDelete
  5. amazing program . GREAT WORK GUYS !!!!

    ReplyDelete
  6. RAMBABU TATYASAHEB INGLE12/11/12, 11:37 AM

    program changla ahe . akdum mast

    ReplyDelete
  7. I think The for loop is being started with i=1 instead of i=0. .dats y the program is ok!

    ReplyDelete
    Replies
    1. actually the program has to be started for i=1 ...because in insertion sort we always compare with the element before it.in case of the 0th element there is nothing to compare with as there is nothing before it. so thats why the loop is ok

      Delete
  8. good work!!!!!

    ReplyDelete
  9. u have taken an arry of 3 but u want to take 5 element. how is it possible????

    ReplyDelete
    Replies
    1. ur array will take only 3 elements and the remaining 2 elements wwill get stored in successive locations in memory following ur array!

      Delete
  10. Well, if you want to use for loop instead of while, it goes something like this-

    for(i=1;i0;j--)
    { if(a[j]<a[j-1])
    { temp=a[j];
    a[j]=a[j-1];
    a[j-1]=temp;
    }
    }
    }
    The rest is the same :)

    ReplyDelete
    Replies
    1. cool..! and correct

      Delete
  11. I thnk its bst way to impliment dir sorting.

    ReplyDelete
  12. #include
    #include
    void main()
    {
    int a[20],k,n,i=0,j=0,t=0;

    clrscr();
    printf("no of of elemts");
    scanf("%d",&n);
    printf("enter %d values",n);
    for(i=0;i=1;j--)
    {

    printf("i=%d j=%d %d %d",i,j,a[j],a[j-1]);
    if(a[j]>a[j-1])
    break;
    else
    {
    t=a[j];
    a[j]=a[j-1];
    a[j-1]=t;
    }

    } printf("\n");
    } printf("\n");


    for(i=0;i<n;i++)
    printf(" %d ",a[i]);
    getch();
    }


    THE ABOVE CODE REPRESENTS WHICH SORTING TECHNIQUE????

    DOES IT REPRESENT INSERTION SORT????

    ReplyDelete