2.15.2012

Insertion sorting using function

Q. write a C program for insertion sorting method using function.


Ans.


/* c program for insertion sorting method using function */
#include<stdio.h>
#include<conio.h>
void insertion(int [], int );
int main()
{
 int arr[30];
 int i,size;
 printf("\n\t------- Insertion sorting using function -------\n\n");
 printf("Enter total no. of elements : ");
 scanf("%d",&size);
 for(i=0; i<size; i++)
 {
    printf("Enter %d element : ",i+1);
    scanf("%d",&arr[i]);
 }
 insertion(arr,size);
 printf("\n\t------- Insertion sorted elements using function -------\n\n");
 for(i=0; i<size; i++)
    printf(" %d",arr[i]);
 getch();
 return 0;
}
void insertion(int arr[], int size)
{
 int i,j,tmp;
 for(i=0; i<size; i++)
 {
   for(j=i-1; j>=0; j--) 
   {
    if(arr[j]>arr[j+1])
    {
      tmp=arr[j];
      arr[j]=arr[j+1];
      arr[j+1]=tmp;
    }
    else
      break;
   }
 }
}


/*********** OUTPUT ****************/
Insertion sorting with screen shot



Related programs:


  1. Heap sorting method and algorithm
  2. Heap sorting
  3. Bubble sorting
  4. Selection Sorting
  5. Insertion sorting
  6. Shell sorting
  7. Quick sorting
  8. Merge sorting
  9. Radix sorting
  10. Liner sorting

No comments:

Post a Comment